日期:2014-05-16  浏览次数:20447 次

Struts2----带参数转发到JSP

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="/user" extends="struts-default">

		<action name="user" class="com.pegasus.web.UserAction">
			<!-- 1.result 的类型为 rederect -->
			<!-- 2.转发到的地址后边加上参数 -->
			<result type="redirect">/user_success.jsp?t=${type}</result>
		</action>
	</package>

</struts>
?

index.jsp

?

<%@ 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>
向结果传参数
<ol>
	<li><a href="user/user?type=1">传参数</a></li>
</ol>
	
</body>
</html>
?

user_success.jsp

?

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
    <%@taglib uri="/struts-tags" prefix="s" %>
<!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>UserSuccess</title>
</head>
<body>
	User Success!
	<!-- 通过<s:property>接受并处理参数 -->
	from valuestack: <s:property value="t"/><br/>
	<!--  -->
	from actioncontext: <s:property value="#parameters.t"/>
	<s:debug></s:debug>
</body>
</html>
?

UserAction.java

?

package com.pegasus.web;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	private int type;
	
	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String execute() throws Exception {
		return "success";
	}

}