提交页面主要代码:
<form method="post" action="messageAction">
<input type="text" name="userName" value="userName Andy"/><br/>
<input type="submit" value="提交"/>
</form>
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
action类:
[java]view plaincopyprint?
package action;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
publicclass MessageAction extends ActionSupport{;
private String userName; //变量名要和表单的名字一样,这样才能自动接收表单数据
privatestaticfinallong serialVersionUID = 1L;
//方法get和set不能少,不然会取不到表单数据。
public String getUserName() {
return userName;
}
publicvoid setUserName(String userName) {
this.userName = userName;
}
public String execute(){
System.out.println("execute");
System.out.println(userName);
ServletActionContext.getRequest().setAttribute("info",userName);
return SUCCESS;
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
struts.xml配置如下:
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="messageAction" class="action.MessageAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
接收页面主要代码:
<body>
info=${requestScope.info }<br/>
</body>
效果截图如下:
--转自
该贴由koei123转至本版2015-7-14 11:08:52