struts2 session 1(通过ActionContext class中的方法getSession得到)

来源:互联网 发布:大数据分析技术现状 编辑:程序博客网 时间:2024/05/21 17:55

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee           http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><display-name>StrutsTutorial</display-name><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>  

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><package name="login" extends="struts-default"><action name="test" class="action.TestAction"><result name="success">/show.jsp</result></action></package></struts>  

java

package action;import java.util.Map;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class TestAction extends ActionSupport{@Overridepublic String execute(){ActionContext actionContext = ActionContext.getContext();       Map session = actionContext.getSession();       session.put("USER_NAME", "Test User");       return SUCCESS;   }}


jsp


<%@ page contentType="text/html; charset=utf-8" language="java"%><%@taglib prefix="s" uri="/struts-tags"%><html><head></head><body><s:property value="#session.USER_NAME"/></body></html>


原创粉丝点击