首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

struts2透过result传递参数

2012-11-05 
struts2通过result传递参数1.使用redirect类型的result通过URL传递参数,也可以使用${属性名}动态设置参数;

struts2通过result传递参数
1.使用redirect类型的result通过URL传递参数,也可以使用${属性名}动态设置参数;
2.在JSP中使用<s:property value="#parameters.属性名"/>显示接收的参数;
3.通过redirect类型的result传递的参数,不能使用<s:property value="属性名"/>显示接收的参数;
    因为redirect不通过Action,ValueStack为空,只能从#parameters中取得参数;

实例:

web.xml:
view source
print?
01.<?xml version="1.0" encoding="UTF-8"?>
02.<web-app version="2.5"
03.    xmlns="http://java.sun.com/xml/ns/javaee"
04.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
05.    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee ;
06.    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
07.  <welcome-file-list>
08.    <welcome-file>hello.jsp</welcome-file>
09.  </welcome-file-list>
10.  <filter>
11.    <filter-name>struts2</filter-name>
12.    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
13.  </filter>
14.  <filter-mapping>
15.    <filter-name>struts2</filter-name>
16.    <url-pattern>/*</url-pattern>
17.  </filter-mapping>
18.</web-app>


struts.xml:
view source
print?
01.<?xml version="1.0" encoding="UTF-8" ?>
02.<!DOCTYPE struts PUBLIC
03.    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04.    "http://struts.apache.org/dtds/struts-2.0.dtd">
05.
06.<struts>
07.    <!--
08.    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
09.    <constant name="struts.devMode" value="false" />
10.
11.    <include file="example.xml"/>
12.
13.
14.
15.    <package name="default" namespace="/" extends="struts-default">
16.        <default-action-ref name="index" />
17.        <action name="index">
18.            <result type="redirectAction">
19.                <param name="actionName">HelloWorld</param>
20.                <param name="namespace">/example</param>
21.            </result>
22.        </action>
23.    </package>
24.     -->
25.
26.    <!-- Add packages here -->
27.    <constant name="struts.devMode" value="true" />
28.    <constant name="struts.i18n.encoding" value="GBK"></constant>
29.     <package name="user" namespace="/" extends="struts-default">
30.        <action name="user" pageEncoding="GB18030"%>
02.<%
03.String path = request.getContextPath();
04.String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
05.%>
06.
07.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
08.<html>
09.  <head>
10.    <base href="<%=basePath%>">
11.    
12.    <title>ResultParam</title>
13.    <meta http-equiv="pragma" content="no-cache">
14.    <meta http-equiv="cache-control" content="no-cache">
15.    <meta http-equiv="expires" content="0">   
16.    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17.    <meta http-equiv="description" content="This is my page">
18.    <!--
19.    <link rel="stylesheet" type="text/css" href="styles.css">
20.    -->
21.  </head>
22.  
23.  <body>
24.      User:<br />
25.    <form action="user!add" method="post">
26.        UserName: <input type="text" name="userName" />
27.        <input type="submit" value="Submit" />
28.    </form>
29.  </body>
30.</html>


addSuccess.jsp:
view source
print?
01.<%@ page language="java" pageEncoding="GB18030"%>
02.<%@ taglib uri="/struts-tags" prefix="s" %>
03.<%
04.String path = request.getContextPath();
05.String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
06.%>
07.
08.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
09.<html>
10.  <head>
11.    <base href="<%=basePath%>">
12.    
13.    <title>Dynamic</title>
14.    <meta http-equiv="pragma" content="no-cache">
15.    <meta http-equiv="cache-control" content="no-cache">
16.    <meta http-equiv="expires" content="0">   
17.    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18.    <meta http-equiv="description" content="This is my page">
19.    <!--
20.    <link rel="stylesheet" type="text/css" href="styles.css">
21.    -->
22.  </head>
23.  
24.  <body>
25.    User Add Success! <br />
26.      UserName:<s:property value="userName"/><br />
27.      UserName:<s:property value="#parameters.userName"/>
28.      <s:debug></s:debug>
29.  </body>
30.</html>

热点排行