关于struts2对form表单和ajax请求中参数的ognl注入
实体Model
public class Model {
private String string;
private int integer;
private float floatNum;
private boolean bl;
/**
* @return the floatNum
*/
public float getFloatNum() {
return floatNum;
}
/**
* @param floatNum the floatNum to set
*/
public void setFloatNum(float floatNum) {
this.floatNum = floatNum;
}
public void setString(String string) {
this.string = string;
}
/**
* @return the integer
*/
public int getInteger() {
return integer;
}
/**
* @param integer the integer to set
*/
public void setInteger(int integer) {
this.integer = integer;
}
/**
* @return the bl
*/
public boolean getBl() {
return bl;
}
/**
* @param bl the bl to set
*/
public void setBl(boolean bl) {
this.bl = bl;
}
/**
* @return the string
*/
public String getString() {
return string;
}
action
public class TestAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 8813730529644729302L;
private Model model;
private float floatNum;
private boolean bl;
private int integer;
public void setInteger(int integer) {
this.integer = integer;
}
public void setBl(boolean bl) {
this.bl = bl;
}
public void setFloatNum(float floatNum) {
this.floatNum = floatNum;
}
public void setModel(Model model) {
this.model = model;
}
private String string;
public void setString(String string) {
this.string = string;
}
public void test() {
JSONObject jsonObj=new JSONObject();
if(model!=null)
{
System.out.println("model中的:"+model.getString()+" | "+model.getInteger()+" |"+model.getFloatNum()+" |"+model.getBl());
}
System.out.println("action中的 :"+string+" |"+integer+"|"+floatNum+"| "+bl);
System.out.println("---------------------------------");
Enumeration<?> enu = ServletActionContext.getRequest()
.getParameterNames();
while (enu.hasMoreElements()) {
String paraName = (String) enu.nextElement();
System.out.println(paraName + ": "
+ ServletActionContext.getRequest().getParameter(paraName));
}
RequestDispatcher rd=ServletActionContext.getRequest().getRequestDispatcher("/呵呵.jsp");
try {
rd.forward(ServletActionContext.getRequest(),ServletActionContext.getResponse());
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("============================");
}
}
jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function(){
$('button').click(function(){
$.post('<%=basePath%>test.action',{'model.string':'字符串','model.floatNum':3.1415,'model.integer':1,'model.bl':true},function(data){
$('div').text(data);
});
});
});
</script>
</head>
<body>
<form action="<%=basePath%>/test.action" method="post">
<input type="text" name="model.string" value="字符串">
<input type="text" name="model.floatNum" value="3.1415">
<input type="text" name="model.integer" value="1">
<input type="text" name="model.bl" value="true"/>
<input type="submit" value="点我"/>
</form>
<button>按钮</button>
</body>
</html>
model中的:字符串 | 0 |0.0 |false
action中的 :null |0|0.0| false
---------------------------------
model.floatNum: 3.1415
model.integer: 1
model.bl: true
model.string: 字符串