日期:2014-05-16  浏览次数:20435 次

json+Struts简单使用例子

1:添加json的jar包...jsonplugin-0.34.jar

2.Struts.xml配置如下

<package name="json" extends="json-default">

       <action name="listuser" class="List">
            <result type="json" />
       </action>
</package>

?3.action配置

public String getUserName() {

		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getJs() {
		return js;
	}
	public void setJs(String js) {
		this.js = js;
	}
	public String getHello() {
		return hello;
	}
	public void setHello(String hello) {
		this.hello = hello;
	}
	

?4.jsp页面:

<html>

  <head>
    <base href="<%=basePath%>">
    <title>Josn</title>
    
    <script type="text/javascript" src="jquery-1.4.2.js"></script>
    <script type="text/javascript">
   	      function validate(){
	    	  alert($("#userName").val());
	    	   jQuery.post("listuser.action",{"userName":$("#userName").val()},function (json){
	    	     alert(json.hello);
	    	     $("#message").text(json.hello);
	    	    },"json");
	    	  }
    </script>
  </head>
  
  <body>
 	  用户名:<input type="text" id="userName"><button onclick="validate()">验证</button><br><br/><br>
    <font id="message"></font>
  </body>
</html>
?