struts实例

本文介绍了一个使用Struts2框架的示例项目,包括配置文件struts.xml的设置,以及多个Action类如何获取不同作用域内的数据,如request、session及application等。

 

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 能响应的扩展名action,do,表示可以是扩展名action,do,和没有扩展名 -->
		<constant name="struts.action.extension" value="action,do,"></constant>
		<package name="default" extends="struts-default">
			<action name="TestActionContextAction" class="com.huangliusong.actioncontext.TestActionContextAction">
				<result>/WEB-INF/textContext.jsp</result>
			</action>
			<action name="TestAwareAction" class="com.huangliusong.actioncontext.TestAwareAction">
				<result>/WEB-INF/aware.jsp</result>
			</action>
			<action name="TestActionContext" class="com.huangliusong.actioncontext.TestActionContext">
			</action>
			<action name="TestServletAwareAction" class="com.huangliusong.actioncontext.TestServletAwareAction">
				<result>/WEB-INF/success.jsp</result>
			</action>
		</package>
</struts>    

TestActionContext.java

package com.huangliusong.actioncontext;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

public class TestActionContext {
	public String execute() {
		/**
		 * 常用的方法:
		 * 1.获取HttpServeletRequest:
		 * 2.获取HttpSession
		 * 3.ServletContext
		 */
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = ServletActionContext.getRequest().getSession();
		ServletContext servletContext = ServletActionContext
				.getServletContext();
		System.err.println("execute...");
		return "success";
	}
}


TestActionContextAction.java

package com.huangliusong.actioncontext;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

public class TestActionContextAction {
	public String execute() {
		// 1.获取一个application对应的map 并向其中添加一个属性
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> applicationMap = actionContext.getApplication();
		applicationMap.put("applicationKey", "applicationValue");
		// 获取属性
		Object date = applicationMap.get("date");
		System.err.println("date" + date);
		// 2.session对应的属性
		Map<String, Object> sessionMap = actionContext.getSession();
		sessionMap.put("sessionKey", "sessionValue");
		// 3request对应的map的属性
		/*
		 * 需要手工调用get()方法 传入request字符串来获取
		 */
		Map<String, Object> requestMap = (Map<String, Object>) actionContext
				.get("request");
		requestMap.put("requestKey", "requestValue");
		// 4.获取请求参数对应的map 并获取指定的参数值 获取请求参数的值
		Map<String, Object> parameters = actionContext.getParameters();
		System.err.println(((String[]) parameters.get("name"))[0]);
		//parameters只能读 不能写入
		parameters.put("age", 12);
		return "success";
	}
}


TestAwareAction.java

package com.huangliusong.actioncontext;

import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;

public class TestAwareAction implements ApplicationAware {
	public String execute(){
		//1.向application中加入一个属性 applicationKey-applicationValue
		application.put("applicationKey2", "applicationValue2");
		//2.从application中读取一个属性date 并且打印	
		System.err.println(application.get("date"));
		//3.
		return "success";
	}
	private Map<String, Object> application;
	@Override
	public void setApplication(Map<String, Object> application) {
		this.application=application;
	}
}


TestServletAwareAction.java

package com.huangliusong.actioncontext;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;
/**
 * 通过实现ServletXxxA	ware 接口方式可以由Strust2接入
 * 需要Servlet相关对象
 * 
 * servletRequestAware:注入HttpServletRequest对象 常用 
 * ServletContextAware:注入ServletContext对象 常用 
 * ServletResponseAware:注入HttpServletResponse对象
 * 
 * @author
 *
 */
public class TestServletAwareAction implements ServletRequestAware,
		ServletContextAware, Servlet, ServletResponseAware {
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public ServletConfig getServletConfig() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getServletInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub

	}

	@Override
	public void service(ServletRequest request, ServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

	}
	private ServletContext context;
	@Override
	public void setServletContext(ServletContext context) {
		// TODO Auto-generated method stub
		this.context=context	;
		System.err.println(context);
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		System.err.println(request);
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		System.err.println(response);
	}

	public String execute(){
		System.err.println("######context#######"+context);
		return "success";
	}
}

aware.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'aware.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    application:${applicationScope.applicationKey2 }
  </body>
</html>


success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'success.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
 success
  </body>
</html>


textContext.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'textContext.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <h1>applicationValue:${applicationScope.applicationKey }</h1><br>
    <h1>applicationValue:${sessionScope.sessionKey }</h1><br>
    <h1>applicationValue:${requestScope.requestKey }</h1><br>
    <h1>age:${parameters.age }</h1><br>
  </body>
</html>


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>strust2-4</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>index</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
  </head>
  
  <body>
  	<div>
  		<a href="TestActionContextAction.action?name=hls">TestActionContextAction</a>
  		<%
    	if(application.getAttribute("date")==null){
    		application.setAttribute("date", new Date());
    	}
     %>
     <a href="TestAwareAction.action?name=hls">TestAwareAction</a>
     <br>
     <a href="TestActionContext.action?name=hls">TestActionContext</a>
     <br>
     <a href="TestServletAwareAction.action?name=hls">TestServletAwareAction</a>
     
  	</div>
  	<div style="margin-top:100px;">
  		<a href="TestActionContextAction.do?name=hls">TestActionContextAction</a>
     <a href="TestAwareAction.do?name=hls">TestAwareAction</a>
     <br>
     <a href="TestActionContext.do?name=hls">TestActionContext</a>
     <br>
     <a href="TestServletAwareAction.do?name=hls">TestServletAwareAction</a>
     
  	</div>
  </body>
</html>

 

转载于:https://my.oschina.net/liusonghuang/blog/828742

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值