接Struts2专题上一篇
1、Action的定义,以及mapping映射描述,看代码注释
Action在MVC中充当Controller控制器的角色,Struts2定义一个Action类通常继续ActionSupport类。
package cn.imethan.web.struts2; import com.opensymphony.xwork2.ActionSupport; /** * HelloWorldAction.java * * @author Ethan Wong * @since JDK 1.7 * @datetime 2015年12月31日上午9:39:42 */ public class HelloWorldAction extends ActionSupport { private static final long serialVersionUID = 1L; private MessageStore messageStore; public String execute() throws Exception { messageStore = new MessageStore() ; return SUCCESS;//对应配置文件里面的 "success" } public MessageStore getMessageStore() { return messageStore; } public void setMessageStore(MessageStore messageStore) { this.messageStore = messageStore; } }
<!-- method方法指定Action调用执行的方法名称 --> <action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute"> <!-- result的名称指定返回名称,对用配置的jsp页面 --> <result name="success">/WEB-INF/view/HelloWorld.jsp</result> </action>
2、Action中添加私有变量,处理from或者url提交的参数
在HelloWorldAction中添加如下私有变量userName和get、set方法
private String userName; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; }
index.jsp添加如下表单内容:
<s:form action="hello"> <s:textfield name="userName" label="Your name" /> <s:submit value="Submit" /> </s:form>
HelloWorld.jsp添加userName展现内容:
<p>userName:<s:property value="userName"/> </p>
部署运行项目,提交表单参数,执行name为hello的Action,浏览器返回结果如下:
userName:imethan,其中imethan是输入表单提交的内容。
3、通过创建Model类,在Model类中设置变量,用于接收表单参数
创建PersonModel类、RegisterAction Action类、register.jsp注册表单页面和成功返回页面thankyou.jsp、struts.xml配置文件添加Action节点
public class Person { private String firstName; private String lastName; private String email; private int age; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString() { return "First Name: " + getFirstName() + " Last Name: " + getLastName() + " Email: " + getEmail() + " Age: " + getAge(); } }
RegisterAction
import com.opensymphony.xwork2.ActionSupport; public class RegisterAction extends ActionSupport { private static final long serialVersionUID = 1L; private Person personBean; public String register(){ return "register"; } @Override public String execute() throws Exception { //call Service class to store personBean's state in database return SUCCESS; } public Person getPersonBean() { return personBean; } public void setPersonBean(Person person) { personBean = person; } }
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Register</title> </head> <body> <h3>Register for a prize by completing this form.</h3> <s:form action="register"> <s:textfield name="personBean.firstName" label="First name" /> <s:textfield name="personBean.lastName" label="Last name" /> <s:textfield name="personBean.email" label ="Email"/> <s:textfield name="personBean.age" label="Age" /> <s:submit/> </s:form> </body> </html>
thankyou.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Registration Successful</title> </head> <body> <h3>Thank you for registering for a prize.</h3> <p>Your registration information: <s:property value="personBean" /> </p> <p><a href="<s:url action='enter' />" >Return to home page</a>.</p> </body> </html>
<action name="enter" class="cn.imethan.web.struts2.RegisterAction" method="register"> <result name="register">/WEB-INF/view/register.jsp</result> </action> <action name="register" class="cn.imethan.web.struts2.RegisterAction" method="execute"> <result name="success">/WEB-INF/view/thankyou.jsp</result> </action>
创建完成后,部署运行,http://localhost:8080/enter.action,输入表单信息如下:
Register for a prize by completing this form.
提交返回信息如下:
Thank you for registering for a prize.
Your registration information: First Name: ethan Last Name: wong Email: ethan@qq.com Age: 100
参考文档