struts2 session 3(利用request获取session)

来源:互联网 发布:云计算的典型应用案例 编辑:程序博客网 时间:2024/05/21 17:18

web.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     
  5.         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.   
  7.   
  8.     <display-name>StrutsTutorial</display-name>  
  9.   
  10.     <filter>  
  11.         <filter-name>struts2</filter-name>  
  12.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  13.     </filter>  
  14.   
  15.     <filter-mapping>  
  16.         <filter-name>struts2</filter-name>  
  17.         <url-pattern>/*</url-pattern>  
  18.     </filter-mapping>  
  19.   
  20.     <welcome-file-list>  
  21.         <welcome-file>index.jsp</welcome-file>  
  22.     </welcome-file-list>  
  23. </web-app>    

struts.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8" ?>    
  2.     <!DOCTYPE struts PUBLIC    
  3.         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    
  4.         "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.     <package name="login" extends="struts-default">  
  8.         <action name="test" class="action.TestAction">  
  9.             <result name="success">/show.jsp</result>  
  10.         </action>  
  11.     </package>  
  12. </struts>    

java

package action;import java.util.Map;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import org.apache.struts2.interceptor.SessionAware;import com.opensymphony.xwork2.ActionSupport;public class TestAction extends ActionSupport  {public String execute() {HttpServletRequest request = ServletActionContext.getRequest();request.getSession().setAttribute("USER_NAME", "Test User 2");return SUCCESS;}}



jsp


[html] view plaincopy
  1. <%@ page contentType="text/html; charset=utf-8" language="java"%>  
  2. <%@taglib prefix="s" uri="/struts-tags"%>  
  3. <html>  
  4.     <head>  
  5.     </head>  
  6.     <body>  
  7.         <s:property value="#session.USER_NAME"/>  
  8.     </body>  
  9. </html>  

原创粉丝点击