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>
<constant name="struts.devMode" value="true"></constant>
<package name="user" namespace="/user" extends="struts-default">
<action name="user" class="com.pegasus.web.UserAction">
<!-- 动态获得跳转的位置 -->
<result>${r}</result>
</action>
</package>
</struts>
UserAction.java
package com.pegasus.web;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private int type;
private String r;
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getR() {
return r;
}
public void setR(String r) {
this.r = r;
}
public String execute() {
System.out.println("type:" + type);
//在这里定义要跳转的页面的地址
if (type == 1)
r = "/user_success.jsp";
else if (type == 2)
r = "/user_error.jsp";
return "success";
}
}
本文介绍了一个Struts2应用的配置文件struts.xml的内容,并展示了如何通过UserAction类来实现根据不同的条件跳转到不同的页面。通过设置属性type,UserAction能够动态决定返回的视图。
701

被折叠的 条评论
为什么被折叠?



