Struts2的动态result

可以根据参数的不同,动态的跳转页面,看如下示例:

(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的值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值