<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.i18n.encoding" value="utf-8"></constant> <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant> <package name="init" namespace="/init" extends="struts-default"> <global-results> <result name="global">/global.jsp</result> </global-results> </package> <package name="user" namespace="/user" extends="init"> <action name="userAction" class="action.UserAction"> <!-- 转发到指定jsp --> <result name="ognl" type="dispatcher">/ognl.jsp</result> <!-- 重定向到指定jsp --> <result name="success" type="redirect">/success.jsp</result> </action> </package> </struts>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="test" namespace="/test" extends="init"> <action name="*Action" class="action.TestAction1" method="{1}"> <!-- 转发到指定jsp --> <result name="dispatcher" type="dispatcher">/success.jsp</result> <!-- 重定向指定 jsp --> <result name="redirect" type="redirect">/success.jsp</result> <!-- 转发到指定Action --> <result name="chain" type="chain">dispatcherTestAction2</result> <!-- 重定向到Action --> <result name="redirectAction" type="redirectAction">redirectTestAction2</result> </action> <action name="*TestAction2" class="action.TestAction2" method="{1}"> <result name="dispatcher" type="dispatcher">/success.jsp</result> <result name="redirect" type="redirect">/success.jsp?remark=${remark}</result> </action> <action name="dynamicAction" class="action.DynamicAction"> <result>${forward}</result> </action> </package> </struts>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.i18n.encoding" value="utf-8"></constant> <include file="user.xml"></include> <include file="test.xml"></include> </struts>
package action; import com.opensymphony.xwork2.ActionSupport; public class DynamicAction extends ActionSupport { private String forward; public String test(){ return SUCCESS; } public String getForward() { return "/"+forward; } public void setForward(String forward) { this.forward = forward; } }
package action; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import org.apache.struts2.interceptor.SessionAware; import org.apache.struts2.util.ServletContextAware; import com.opensymphony.xwork2.ActionSupport; public class TestAction1 extends ActionSupport implements ServletRequestAware, ServletResponseAware, SessionAware, ServletContextAware { private HttpServletRequest request; private HttpServletResponse response; private Map<String, Object> session; private ServletContext application; private HttpSession session2; private String name; private int age; private String remark; public String dispatcher() { remark = "转发到指定jsp"; System.out.println("name=[" + name + "]"); System.out.println("age=[" + age + "]"); System.out.println("remark=[" + remark + "]"); return "dispatcher"; } public String redirect() { remark = "重定向到指定jsp"; System.out.println("name=[" + name + "]"); System.out.println("age=[" + age + "]"); System.out.println("remark=[" + remark + "]"); request.setAttribute("name", name); request.setAttribute("age", age); request.setAttribute("remark", remark); session2 = request.getSession(); session2.setAttribute("name", name); session2.setAttribute("age", age); session2.setAttribute("remark", remark); /* * session.put("name", name); session.put("age", age); * session.put("remark", remark); */ return "redirect"; } public String chain() { remark = "还在TestAction1"; return "chain"; } public String redirectAct() { remark = "还在TestAction1"; return "redirectAction"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void setServletRequest(HttpServletRequest request) { this.request = request; } public void setServletResponse(HttpServletResponse response) { this.response = response; } public void setSession(Map<String, Object> session) { this.session = session; } public void setServletContext(ServletContext application) { this.application = application; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } }
package action; import java.io.UnsupportedEncodingException; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import org.apache.struts2.interceptor.SessionAware; import org.apache.struts2.util.ServletContextAware; import com.opensymphony.xwork2.ActionSupport; public class TestAction2 extends ActionSupport implements ServletRequestAware, ServletResponseAware, SessionAware, ServletContextAware { private HttpServletRequest request; private HttpServletResponse response; private Map<String, Object> session; private ServletContext application; private String remark; private String chain; public String dispatcher() { System.out.println("TestAction2.dispatcher()"); System.out.println("remark=[" + remark + "]"); remark = "转发到Action"; chain = "chain到此一游"; return "dispatcher"; } public String redirect() { remark = "重定向Action"; try { remark = java.net.URLEncoder.encode(remark, "utf-8"); // remark = java.net.URLEncoder.encode(remark, "utf-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "redirect"; } public void setServletRequest(HttpServletRequest request) { this.request = request; } public void setServletResponse(HttpServletResponse response) { this.response = response; } public void setSession(Map<String, Object> session) { this.session = session; } public void setServletContext(ServletContext application) { this.application = application; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public String getChain() { return chain; } public void setChain(String chain) { this.chain = chain; } }
package action; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import org.apache.struts2.interceptor.SessionAware; import org.apache.struts2.util.ServletContextAware; import com.opensymphony.xwork2.ActionSupport; import entity.Cat; import entity.User; public class UserAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, SessionAware, ServletContextAware { private HttpServletRequest request; private HttpServletResponse response; private Map<String, Object> session; private ServletContext application; private User user; private String title; private List<Cat> cats = new ArrayList<Cat>(); private Map<String, Cat> catMap = new HashMap<String, Cat>(); public String login() { Cat m0 = new Cat("m0", "SSS", 20); Cat m1 = new Cat("m1", "AAA", 20); Cat m2 = new Cat("m2", "BBB", 20); Cat c0 = new Cat(); c0.setName("c0"); c0.setType("SSS"); c0.setAge(10); c0.setMother(m0); Cat c01 = new Cat(); c01.setName("c01"); c01.setType("SSS"); c01.setAge(5); c01.setMother(c0); Cat c02 = new Cat(); c02.setName("c02"); c02.setType("SSS"); c02.setAge(6); c02.setMother(c0); Cat c03 = new Cat(); c03.setName("c03"); c03.setType("SSS"); c03.setAge(7); c03.setMother(c0); c0.getChildren().add(c01); c0.getChildren().add(c02); c0.getChildren().add(c03); Cat c1 = new Cat(); c1.setName("c1"); c1.setType("AAA"); c1.setAge(16); c1.setMother(m1); Cat c11 = new Cat(); c11.setName("c11"); c11.setType("AAA"); c11.setAge(11); c11.setMother(c1); Cat c12 = new Cat(); c12.setName("c12"); c12.setType("AAA"); c12.setAge(13); c12.setMother(c1); Cat c13 = new Cat(); c13.setName("c13"); c13.setType("AAA"); c13.setAge(7); c13.setMother(c1); c1.getChildren().add(c11); c1.getChildren().add(c12); c1.getChildren().add(c13); Cat c2 = new Cat(); c2.setName("c2"); c2.setType("BBB"); c2.setAge(19); c2.setMother(m2); Cat c21 = new Cat(); c21.setName("c21"); c21.setType("BBB"); c21.setAge(15); c21.setMother(c2); Cat c22 = new Cat(); c22.setName("c22"); c22.setType("BBB"); c22.setAge(4); c22.setMother(c2); Cat c23 = new Cat(); c23.setName("c23"); c23.setType("BBB"); c23.setAge(16); c23.setMother(c2); c2.getChildren().add(c21); c2.getChildren().add(c22); c2.getChildren().add(c23); cats.add(c0); cats.add(c1); cats.add(c2); catMap.put("c0", c0); catMap.put("c1", c1); catMap.put("c2", c2); title = "登录成功"; return "ognl"; } public double sum(double a, double b) { return a * b; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public List<Cat> getCats() { return cats; } public void setServletRequest(HttpServletRequest request) { this.request = request; } public void setServletResponse(HttpServletResponse response) { this.response = response; } public void setSession(Map<String, Object> session) { this.session = session; } public void setServletContext(ServletContext application) { this.application = application; } public Map<String, Cat> getCatMap() { return catMap; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators> <field name="user.username"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>请输入用户名</message> </field-validator> </field> <field name="user.username"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>请输入用户密码</message> </field-validator> </field> </validators>