文档章节

struts实例

黄柳淞
 黄柳淞
发布于 2017/01/21 02:10
字数 1037
阅读 11
收藏 0

 

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>

 

© 著作权归作者所有

黄柳淞
粉丝 2
博文 108
码字总数 54251
作品 0
南宁
程序员
私信 提问
struts中Action里的getter()方法是有作用的

我一直以为在struts中,Action里的getter()方法是没用的,昨天发现并非如此。(此处的struts是2.0以上的版本) 当时,我在Action里定义了一个private变量,在前台给该Action传了两个参数,想...

水牛叔叔
2012/07/04
0
0
struts2跟struts1的区别

1) 在Action实现类方面的对比:Struts 1要求Action类继承一个抽象基类;Struts 1的一个具体问题是使用抽象类编程而不是接口。Struts 2 Action类可以实现一个Action接口,也可以实现其他接口,...

长平狐
2013/01/06
43
0
struts1.* 异常处理机制

几个重要类的说明 ActionServlet struts的核心类,用于初始化struts配置文件,处理发送到action的请求; ModuleConfig struts配置文件信息加载的接口,ModuleConfigImpl是接口的默认实现,可...

STG0825
2013/08/29
0
0
Struts 2再曝远程代码执行漏洞S2-037

导读 今年4月份,Apache Stuts 2之上发现的S2-033远程代码执行漏洞,以迅雷不及掩耳之势席卷而来。其利用代码很快就在短时间内迅速传播。而且官方针对这个高危漏洞的修复方案还是无效的。 悲...

linuxprobe
2016/06/22
0
0
Struts 源码学习之ActionServlet ( 一)

Struts 源码最新版本为 struts-1.3.8-src.zip ( 12-Mar-2007 00:06 ) 学习笔记使用struts-1.3.5-src.zip 的源码, 下载地址:http://archive.apache.org/dist/struts/source/ 1. 在web.x......

xiahuawuyu
2012/05/17
0
0

没有更多内容

加载失败,请刷新页面

加载更多

探讨android更新UI的几种方法

作为IT新手,总以为只要有时间,有精力,什么东西都能做出来。这种念头我也有过,但很快就熄灭了,因为现实是残酷的,就算一开始的时间和精力非常充足,也会随着项目的推进而逐步消磨殆尽。我...

天王盖地虎626
53分钟前
3
0
浙江易柏生物技术有限公司年产200万份高危型HPV分型检测试剂盒、200万份高危型HPVE6-E7mRNA荧光检测试剂盒生产线项目环境影响评价公告信息

浙江易柏生物技术有限公司年产200万份高危型HPV分型检测试剂盒、200万份高危型HPVE6-E7mRNA荧光检测试剂盒生产线项目环境影响评价公告信息 一、建设项目基本情况 项目名称:年产200万份高危型...

miaojiangmin
今天
4
0
nproc systemd on CentOS 7

Increasing nproc for processes launched by systemd on CentOS 7 Ask Question I have successfully increased the nofile and nproc value for the local users, but I couldn't find a p......

MtrS
今天
5
0
了解微信小程序下拉刷新功能

小程序提供了这个事件。 onPullDownRefresh() 监听用户下拉刷新事件。 如果要开启下拉刷新功能,要先到json配置: "enablePullDownRefresh":true 配置后下拉有反应了但是没有加载效果,在onP...

oixxan__
今天
2
0
springmvc java对象转json,上传下载(未完)拦截器Interceptor以及源码解析(未完待续)

package com.atguigu.my.controller;import java.util.Collection;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Contr......

architect刘源源
今天
34
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部