struts2的Result配置

在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", "请输入合法的年龄");
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值