在struts-default.xml
<result-types>
<!--配置Action连接结果-->
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<!--配置默认连接结果-->
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
<!--配置freemarker结果类型-->
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<!--配置http请求的类型-->
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<!--配置重定向结果类型-->
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<!--配置重定向action结果类型-->
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<!--配置stream结果类型-->
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
<!--配置velocityResult结果类型-->
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
<!-- Deprecated name form scheduled for removal in Struts 2.1.0. The camelCase versions are preferred. See ww-1707 -->
<result-type name="redirect-action" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="plaintext" class="org.apache.struts2.dispatcher.PlainTextResult" />
</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" class="com.struts2.HelloWorld">
<result name="success" 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" class="com.struts2.HelloWorld" 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", "请输入合法的年龄");
}
}
}