前台js请求及处理
<script type="text/javascript" src="<%=path%>/js/jquery.js"></script>
<script type="text/javascript">
$(function(){
$.getJSON("login.action",{},function(result){
var r = eval(result);//将json字符串转为js对象
alert(r.name);
});
});
</script>
struts.xml配置
<package name="default" namespace="/" extends="json-default">
<action name="login" class="com.liyh.struts.web.HelloJsonAction" method="login">
<result type="json"></result>
</action>
</package>
HelloJsonAction.java代码
public class HelloJsonAction extends ActionSupport
{
private String name;//省略getter和setter
@JSON
public String login()
{
this.name = "dd";
return SUCCESS;
}
}
还有问题要注意:
1、要导入struts2-json-plugin-2.2.1.jar和json-lib-2.1-jdk15.jar