struts2的Result配置
在struts-default.xml
<result-types> <!--配置Action连接结果--> <result-type name="chain" default="true"/> <!--配置freemarker结果类型--> <result-type name="freemarker" /> <!-- Deprecated name form scheduled for removal in Struts 2.1.0. The camelCase versions are preferred. See ww-1707 --> <result-type name="redirect-action" /> </result-types>
struts.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="test" extends="struts-default"> <action name="reg" type="chain">index</result> <result name="ERROR" type="chain"> <param name="actionName">reg</param> </result> <result name="input" type="chain"> <param name="actionName">reg</param> </result> </action> <action name="index" method="goIndex"> <result name="success"> <param name="location">/index.jsp</param> </result> </action> </package> </struts> action package com.struts2; import java.util.Date; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class HelloWorld extends ActionSupport{ /** * */ private static final long serialVersionUID = 1L; private Date date2; private String msg; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Date getDate2() { return date2; } public void setDate2(Date date2) { this.date2 = date2; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } private String username; private int age; @SuppressWarnings("unchecked") public String goIndex(){ // Map<String,String> map=null; // if(this.getUsername().equals("")) // return ERROR; // else { // map=ActionContext.getContext().getSession(); // map.put("username", this.getUsername()); // System.out.println ("姓名"+getUsername()+"\t 年龄:"+this.getAge()+"\t注册日期"+this.getDate2()); // return SUCCESS; // } Map<String,String> map=null; map=ActionContext.getContext().getSession(); map.put("username", this.getUsername()); System.out.println ("姓名"+getUsername()+"\t 年龄:"+this.getAge()+"\t注册日期"+this.getDate2()); return SUCCESS; } @Override public void validate() { if(username.length()>10){ this.addFieldError("username", "用户名太长了"); } if(username.equals("")){ this.addFieldError("username", "用户名不能为空"); } if(age<0||age>150){ this.addFieldError("age", "请输入合法的年龄"); } } }