可以根据参数的不同,动态的跳转页面,看如下示例:
(1)Action
package com.struts2.study.yy;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private String name;
private String page;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
@Override
public String execute() throws Exception {
if (name.equals("zhangsan"))
page = "/zhangsan.jsp";
if (name.equals("lisi"))
page = "/lisi.jsp";
return SUCCESS;
}
}
(2)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" />
<package name="user" namespace="/actions" extends="struts-default">
<action name="user" class="com.struts2.study.yy.UserAction">
<result>${page}</result>
</action>
</package>
</struts>
(3)index.jsp
<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<% String context = request.getContextPath(); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
<a href="actions/user?name=zhangsan">zhangsan</a></br>
<a href="actions/user?name=lisi">lisi</a></br>
</body>
</html>
即根据name值得不同跳转到相应页面。Action中获取name的值,从而给page赋不同的值,在struts.xml文件中,使用${page}动态的获取page的值。