欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Struts2 ActionContext(二十四)

程序员文章站 2022-05-18 12:13:21
...
1.在web中需要访问的Servlet API 是HttpServletRequest,HttpServletSession,ServletContext 这3个类代表了JSP的内置对象的request session application

下面是间接访问Srevlet API(后一章会直接访问Servlet API)
2.ActionContext类
创建实例化对象 是
ActionContext ac=ActionContext.getContext();
在ActionContext中有一些常用的方法:
Map getApplication();
Map getParameters();
Map getSession()
void setAppliaction(Map application)

3.action.java
package com.sh.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
	private String loginName;
	private String loginPassword;

	public String checkLogin(){
		ActionContext ac=ActionContext.getContext();
		Map app=ac.getApplication();
		ac.getSession().put("login", this.loginName);
		ac.getApplication().put("login",this.loginName);
		return SUCCESS;
	}


struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts> 
    <package name="/user" extends="struts-default">
    	<action name="loginAction" class="com.sh.action.LoginAction" method="checkLogin">
    		<result>/index.jsp</result>
    	</action>
    </package>
</struts>



login.jsp
 <body>
    <center>
    	<s:form action="loginAction" method="post">
    		<s:textfield name="loginName" label="loginName" />
    		<s:textfield name="loginPassword" label="loginPassword"/>    	
    		<s:submit key="loginBtn"/>
    		<s:token/>
    	</s:form>
    
    </center>
  </body>

index.jsp
  <body>
   读取session中的信息 <s:property value="#session.login"/>
 读取application中的信息<s:property value="#application.login"/>
  </body>