Struts2中访问Web元素

Struts2中访问Web元素有四种方法,但是一般最常用的就是有"控制反转与依赖注入"思想那种,下面就主要说明最常用的方法:

Struts2中访问Web元素可以从以下三块入手:
   1.Action中向Web元素中添加添加数据:
    a.实现 RequestAware, SessionAware, ApplicationAware接口,通过set方法设置Map类型的Web元素;
    b. 使用put方法向对应的Map对象中添加数据,Struts2将其转换为Web元素;

   2.JSP中取得Web元素中的数据:
    a. 通过<s:property value="" />标签取得相应Web元素中的数据,Web元素名前要加“#”,如#request.key;
    b.也可以直接通过Web元素的getAttribute()方法取得相应的数据。

   3.一般只通过这种方法设置session,Action的属性不需要特殊处理就能直接在JSP中通过request取得。
 

下面举例
   1.web.xml:
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>
---------------------------------------------------------------------------
  2.struts.xml:
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" class="org.shunhui.action.UserAction">
31.            <result>/addSuccess.jsp</result>
32.        </action>
33.    </package>
34.</struts>
-------------------------------------------------------------------------
3.UserAction.java:
01.package org.shunhui.action;
02.
03.import java.util.Map;
04.
05.import org.apache.struts2.interceptor.ApplicationAware;
06.import org.apache.struts2.interceptor.RequestAware;
07.import org.apache.struts2.interceptor.SessionAware;
08.
09.import com.opensymphony.xwork2.ActionSupport;
10.
11.public class UserAction extends ActionSupport implements RequestAware, SessionAware, ApplicationAware {
12.
13.    private String userName;
14.    private Map<String, Object> request;
15.    private Map<String, Object> session;
16.    private Map<String, Object> application;
17.    
18.    public String add() {
19.        request.put("userName", userName);
20.        session.put("session", "123");
21.        application.put("app", "456");
22.        return SUCCESS;
23.    }
24.    
25.    public String getUserName() {
26.        return this.userName;
27.    }
28.    
29.    public void setUserName(String userName) {
30.        this.userName = userName;
31.    }
32.
33.    public void setRequest(Map<String, Object> request) {
34.        this.request = request;
35.    }
36.
37.    public void setSession(Map<String, Object> session) {
38.        this.session = session;
39.    }
40.
41.    public void setApplication(Map<String, Object> application) {
42.        this.application = application;
43.    }
44.}
------------------------------------------------------------------------
4.index.jsp:
01.<%@ page language="java" import="java.util.*" 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>Valiation</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.    <form action="user!add" method="post">
25.        UserName: <input type="text" name="userName" />
26.        <input type="submit" value="Submit" />
27.    </form>
28.  </body>
29.</html>
-------------------------------------------------------------------
5.addSuccess.jsp:
01.<%@ page language="java" import="java.util.*" 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>AddSuccess</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="#request.userName" /><br />
27.    Session: <s:property value="#session.session" /><br />
28.    Application: <s:property value="#application.app" /><br />
29.    <s:debug></s:debug>
30.  </body>
31.</html>

你可能感兴趣的:(apache,Web,jsp,xml,struts)