struts2中XML配置中result的值

来源:互联网 发布:剑网三炮哥捏脸数据 编辑:程序博客网 时间:2024/05/17 20:08

result中的常见值得配置。

1.dispatcher 配置如下所示

<result name="welcome" type="dispatcher">/welcome.jsp</result>

作用:我们在A页面填写相关的数据,提交到后台的Action做以处理,然后将A页面填写的数据返回到相关的welcome页面。也就是说,可以将A页面中的数据传送到welcome页面,其实它只是一个request。

使用场景:在login页面填写的userName和password,在后台的action中验证处理以后,如果正确,页面将跳转到list页面或者其他的页面,但是我们需要login页面上的userName这个信息,所以就可以使用dispatcher这个属性。

**如果写成<result name="welcome" >/welcome.jsp</result>这样,那么这个result默认type=“dispatcher”。

所以很多时候如果你不写type=“dispatcher”时也感觉值从A页面传到了welcome页面,原因就是dispatcher是result的

默认值。

2.redirecter

<result name="welcome" type="redirecter">/welcome.jsp</result>

如果在A页面填写数据,通过action处理以后,到struts.xml的配置文件中,使用redirecter,

那么A页面中填写的数据在welcome页面中是取不到的。


3.chain

<action name="action2" class="com.struts.Action1"

<result name="toSecond" type="chain">action2</result>

</action>

<action name="action2" class="com.struts.Action2"

<result name="toWelcome" type="dispatcher">/welcome.jsp</result>

</action>

从页面A请求道action1中,做以相关的处理,处理完后跳转到action2中,

然后在action2中处理相关的业务,然后再次跳转到welcome页面上。

为什么要这样写,目的就是为了将A页面上填写的相关的值可以在welcome页面上输出。

chain在这里的作用,我个人理解就是为了让从A页面上的request延续到action2中,

这样就不会重新发起一个request,所以A页面上的数据也就不会丢失,从而达到传值的目的。


result还有几种值类型,不常用,就不说了。

看看怎也在welcome.jsp上取login.jsp上的值。

login.jsp

<input type="text" name="account">


action.java

private String account;

getter/setter menthod ;



welcome.jsp

第一种取值方式:

<s:property value="account">

第种取值方式:

${account}






0 0