结果图:
一、Action类
public class TestActinon extends ActionSupport {
public String method() {
this.msg="pkd";
this.message="成功返回单个值!!!";
return SUCCESS;
}
public String method1() {
return SUCCESS;
}
}
二、配置Struts
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="default" namespace="/" extends="json-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="returnmessage" class="com.action.TestActinon">
<result type="json"/>
</action>
</package>
</struts>
三、在html页面通过jqury传递获取单个值
<script type="text/javascript">
$(document).ready(function(){
使用json的方式返回单个值
$("#returnMsg").click(function(){
$.getJSON("returnmessage!method.action",function(data){
alert(data);
alert(data.msg);
alert(data.message);
$("#showdata").html("<font color='red'>"+data.msg+"<br>"+data.message+"</font>");
})
});
//使用json的方式传固定值
$("#returnMsg1").click(function(){
$.getJSON("returnmessage!method1.action",
{msg:"lisi",message:"json传固定值"},function(data){
alert(data);
alert(data.msg);
alert(data.message);
$("#showdata").html("<font color='red'>"+data.msg+"<br>"+data.message+"</font>");
})
});
//使用json的方式传文本框中的变量值
$("#returnMsg2").click(function(){
$.getJSON("returnmessage!method1.action?",{msg:$("#userId").val(),
message:$("#userpwd").val()},function(data){
alert(data);
alert(data.msg);
alert(data.message);
$("#showdata").html("<font color='red'>"+data.msg+"<br>"+data.message+"</font>");
})
});
//使用web方式传文本框中的变量值
$("#returnMsg3").click(function(){
$.getJSON("returnmessage!method1.action?msg="+$("#userId").val()
+"&message="+$("#userpwd").val(),function(data){
alert(data);
alert(data.msg);
alert(data.message);
$("#showdata").html("<font color='red'>"+data.msg+"<br>"+data.message+"</font>");
})
});
});
</script>
</head>
<body>
<center>
<input type="button" value="使用json的方式返回单个值" id="returnMsg"/>
<input type="button" value="使用json的方式传固定值" id="returnMsg1"/>
<input type="button" value="使用json的方式传文本框中的变量值" id="returnMsg2"/>
<input type="button" value="使用web方式传文本框中的变量值" id="returnMsg3"/><br>
<div id="showdata"></div>
<form>
用户名: <input type="text" id="userId" ><br>
密 码:<input type="text" id="userpwd" >
</form>
</center>
</body>
</html>