日期:2014-05-16 浏览次数:20456 次
主要jar包:
springframework?3.1.1版本
struts2?2.3.4.1版本
其他
js包:
jquery.ms.js
?
?
Domain
?
Device2.java:
public class Device2 {
private String deviceId;
private String userName;
private String pwd;
private String netAddr;
private String netPort;
// set/get方法略
}
?
Action类:
@Component
@Scope("prototype")
@SuppressWarnings("serial")
public class MyAction extends ActionSupport{
private Device2 device;
private Integer id;
private String result;
public String add3(){
System.out.println("add3");
System.out.println(device);
// result = "{name:pxj, password:'登录成功!'}";
result = device.toString();
return SUCCESS;
}
// set/get方法略
}
?
struct.xml重点配置如下
?
<package name="struts3" extends="struts-default,json-default" namespace="/">
<action name="myAction3" class="myAction" method="add3">
<result type="json">
<param name="root">result</param>
</result>
</action>
</package>
?
?包必须继承json-default。
result的type为json,其中root参数的值result表示将对应action中属性为result属性值返回给客户端。
?
jsp页面:
?
<form action="myAction3" method="post"> 设备id:<input type="text" name="deviceId3" id="deviceId3" /><br> 登陆账号:<input type="text" name="userName3" id="userName3" /> <br> 登陆密码:<input type="text" name="pwd3" id="pwd3" /> <br> <input type="button" onclick="onclickSave()" value="提交" /> <div id="info"></div> </form>?
js代码如下:
?
?
<script src="jquery/jquery.min.js" type="text/javascript"></script>
<script>
function onclickSave()
{
$.post('myAction3',
{
"device.deviceId":$('#deviceId3')[0].value,
"device.userName":$('#userName3')[0].value,
"device.pwd":$('#pwd3')[0].value
// loginName:$('#userName2')[0].value,
// loginPwd:$('#pwd2')[0].value
},
function(data){
alert(data);//请求返回的内容
},'json');
}
</script>
?
web.XML
?
<context-param>
<param-name> contextConfigLocation </param-name>
<param-value>
/WEB-INF/config/spring_business.xml
</param-value>
</context-param>
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
?
spring_business.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:contex