struts2 session 3(利用request获取session)

web.xml

[html]  view plain copy
  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 plain copy
  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 plain copy
  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>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值