3.3Struct2(上午)的配置和在开发中的应用

2023-11-21 19:38

本文主要是介绍3.3Struct2(上午)的配置和在开发中的应用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

(

struts的配置文件

action的类型

action中方法的调用

Action中如何获取Servlet的对象。

)

1、struts的配置文件

        1.1struts的常量配置:

struts中默认位置:struts-core.jar中/org/apache/struts2/default.properties

struts中读取常量的4个地方:

A:struts-default.xml  struts2-core-2.3.28.jar/struts-default.xml。一般不配置 

B:struts.xml

C:struts.properties src目录下新建一个配置文件。 以key=value方式来配置

D:web.xml

<init-param>

<param-name>struts.i18n.encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>


<init-param>

<param-name>struts.devMode</param-name>

<param-value>false</param-value>

</init-param>

依次读取常量的配置。最后配置会覆盖前面配置的。       

             1.2include配置:导入另一个struts的配置文件。
              多模块或者多个合作的项目。
              <include file="struts-user.xml"></include>
              <include file="struts-notice.xml"></include>

              1.3package的配置

name: 包的名称。必须唯一。为其他package继承使用

namespace命名空间。客户端请求的的命名空间
extend 包之间的继承。
abstract true|falsetrue:表示包是一个抽象包,限制:不可以在抽象包中配置Action.

extend 包之间的继承。

abstract true|falsetrue:表示包是一个抽象包,限制:不可以在抽象包中配置Action.

        1.4action的配置

              name:action的名称
              class:action的class路径
              method:调用action中的哪一个方法。如果不配置默认是execute方法。

              param:在struts.xml中设置(SET)或者获取(GET)Actioin定义的成员变量。

设置:

获取:struts.xml是通过${成员变量名称},来获取成员变量的值。

 result:配置action中返回的result

 result的类型:

chain/dispatcher/redirect/redirectAction/stream/freemarker,默认为dispatch,表示默认是请求转发。

1:dispatch:请求转发到jsp页面

2:redirect:重定向到jsp页面

3:chain:请求转发到另一个Action

4:redirectAction:重定向另一个Action

5:stream:表示返回一个流(文件下载使用)

6:freemarker:表示返回的页面是一个freemarker页面。

2、struts的开发运用

2.1:action的类型
<a href="<%=path%>/type/typeAction_1">A:直接写class</a>
<a href="<%=path%>/type/typeAction_2">B:实现Action接口</a>
<a href="<%=path%>/type/typeAction_3">C:纪承ActionSupport类</a>
2.2:action中方法的调用,调用除了execute的方法
A:使用method属性。
<a href="<%=path%>/method/methodAction_1_add">add</a>
<a href="<%=path%>/method/methodAction_1_save">save</a>
<a href="<%=path%>/method/methodAction_1_update">update</a>
<a href="<%=path%>/method/methodAction_1_delete">delete</a>
B:使用动态方法调用:前提:开启struts.enable.DynamicMethodInvocation常量为true
调用方法:!+方法名称
<a href="<%=path%>/method/methodAction_2!add">add</a>
<a href="<%=path%>/method/methodAction_2!save">save</a>
<a href="<%=path%>/method/methodAction_2!update">update</a>
<a href="<%=path%>/method/methodAction_2!delete">delete</a>
C:使用通配符调用:对第一方法的简化操作。
<a href="<%=path%>/method/methodAction_3_add">add</a>
<a href="<%=path%>/method/methodAction_3_save">save</a>
<a href="<%=path%>/method/methodAction_3_update">update</a>
<a href="<%=path%>/method/methodAction_3_delete">delete</a>

Struts.xml中的配置:

	<package name="meethod" namespace="/method" extends="struts-default"><action name="methodAction_1_add" class="com.action.method.MethodAction_1"method="add"></action><action name="methodAction_1_save" class="com.action.method.MethodAction_1"method="save"></action><action name="methodAction_1_update" class="com.action.method.MethodAction_1"method="update"></action><action name="methodAction_1_delete" class="com.action.method.MethodAction_1"method="delete"></action><action name="methodAction_2" class="com.action.method.MethodAction_2"></action><action name="methodAction_3_*" class="com.action.method.MethodAction_3"method="{1}"></action></package>

2.3:Action中如何获取Servlet的对象。
<a href="<%=path%>/servlet/servletAction_1">A:使用ServletActionContext这个类来获取</a>
<a href="<%=path%>/servlet/servletAction_2">B:让Action实现Servlet拦截器(struts框架提供)的接口</a>

A:

package com.action.servlet;import java.io.PrintWriter;import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class ServletAction_1 extends ActionSupport {@Overridepublic String execute() throws Exception {/*** 1:使用ServletActionContext这个类来获取*/HttpServletRequest request = ServletActionContext.getRequest();HttpServletResponse response = ServletActionContext.getResponse();ServletContext servletContext = ServletActionContext.getServletContext();HttpSession session = request.getSession();response.setCharacterEncoding("UTF-8");response.setContentType("text/html");PrintWriter out = response.getWriter();System.out.println(request.getContextPath());System.out.println(servletContext.getRealPath("/index.jsp"));System.out.println(session.getId());out.println("使用out往页面中往出内容");out.print("xxxxx");out.flush();out.close();return NONE;}
}
B:

package com.action.servlet;import java.io.PrintWriter;import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;import com.opensymphony.xwork2.ActionSupport;public class ServletAction_2 extends ActionSupport implementsServletRequestAware, ServletResponseAware, ServletContextAware {private HttpServletRequest request;private HttpServletResponse response;private ServletContext servletContext;private HttpSession session;@Overridepublic void setServletRequest(HttpServletRequest request) {this.request = request;if (this.request != null) {this.session = this.request.getSession();}}@Overridepublic void setServletResponse(HttpServletResponse response) {this.response = response;}@Overridepublic void setServletContext(ServletContext servletContext) {this.servletContext = servletContext;}@Overridepublic String execute() throws Exception {response.setCharacterEncoding("UTF-8");response.setContentType("text/html");PrintWriter out = response.getWriter();out.println(request.getContextPath() + "<br/><br/>");out.println(servletContext.getRealPath("/WEB-INF/lib/web.xml")+ "<br/><br/>");out.println("sessionID = " + session.getId() + "<br/><br/>");out.println("使用out往页面中往出内容" + "<br/><br/>");out.print("xxxxx" + "<br/><br/>");out.flush();out.close();return NONE;}}

通过以上两种方法,我们可以得出一个抽象的工具类,到时候所有的action类只要继承这个BaseAction类就好了。

package com.util;import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;import com.opensymphony.xwork2.ActionSupport;public abstract class BaseAction extends ActionSupport implementsServletRequestAware, ServletResponseAware, ServletContextAware {protected HttpServletRequest request;protected HttpServletResponse response;protected ServletContext servletContext;protected HttpSession session;@Overridepublic void setServletRequest(HttpServletRequest request) {this.request = request;if (this.request != null) {this.session = this.request.getSession();}}@Overridepublic void setServletResponse(HttpServletResponse response) {this.response = response;}@Overridepublic void setServletContext(ServletContext servletContext) {this.servletContext = servletContext;}@Overridepublic abstract String execute() throws Exception;}



这篇关于3.3Struct2(上午)的配置和在开发中的应用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/404608

相关文章

Spring配置扩展之JavaConfig的使用小结

《Spring配置扩展之JavaConfig的使用小结》JavaConfig是Spring框架中基于纯Java代码的配置方式,用于替代传统的XML配置,通过注解(如@Bean)定义Spring容器的组... 目录JavaConfig 的概念什么是JavaConfig?为什么使用 JavaConfig?Jav

Spring Boot Interceptor的原理、配置、顺序控制及与Filter的关键区别对比分析

《SpringBootInterceptor的原理、配置、顺序控制及与Filter的关键区别对比分析》本文主要介绍了SpringBoot中的拦截器(Interceptor)及其与过滤器(Filt... 目录前言一、核心功能二、拦截器的实现2.1 定义自定义拦截器2.2 注册拦截器三、多拦截器的执行顺序四、过

springboot的controller中如何获取applicatim.yml的配置值

《springboot的controller中如何获取applicatim.yml的配置值》本文介绍了在SpringBoot的Controller中获取application.yml配置值的四种方式,... 目录1. 使用@Value注解(最常用)application.yml 配置Controller 中

springboot中配置logback-spring.xml的方法

《springboot中配置logback-spring.xml的方法》文章介绍了如何在SpringBoot项目中配置logback-spring.xml文件来进行日志管理,包括如何定义日志输出方式、... 目录一、在src/main/resources目录下,也就是在classpath路径下创建logba

Python+wxPython开发一个文件属性比对工具

《Python+wxPython开发一个文件属性比对工具》在日常的文件管理工作中,我们经常会遇到同一个文件存在多个版本,或者需要验证备份文件与源文件是否一致,下面我们就来看看如何使用wxPython模... 目录引言项目背景与需求应用场景核心需求运行结果技术选型程序设计界面布局核心功能模块关键代码解析文件大

C++多线程开发环境配置方法

《C++多线程开发环境配置方法》文章详细介绍了如何在Windows上安装MinGW-w64和VSCode,并配置环境变量和编译任务,使用VSCode创建一个C++多线程测试项目,并通过配置tasks.... 目录下载安装 MinGW-w64下载安装VS code创建测试项目配置编译任务创建 tasks.js

Nginx概念、架构、配置与虚拟主机实战操作指南

《Nginx概念、架构、配置与虚拟主机实战操作指南》Nginx是一个高性能的HTTP服务器、反向代理服务器、负载均衡器和IMAP/POP3/SMTP代理服务器,它支持高并发连接,资源占用低,功能全面且... 目录Nginx 深度解析:概念、架构、配置与虚拟主机实战一、Nginx 的概念二、Nginx 的特点

2025最新版Android Studio安装及组件配置教程(SDK、JDK、Gradle)

《2025最新版AndroidStudio安装及组件配置教程(SDK、JDK、Gradle)》:本文主要介绍2025最新版AndroidStudio安装及组件配置(SDK、JDK、Gradle... 目录原生 android 简介Android Studio必备组件一、Android Studio安装二、A

Nginx内置变量应用场景分析

《Nginx内置变量应用场景分析》Nginx内置变量速查表,涵盖请求URI、客户端信息、服务器信息、文件路径、响应与性能等类别,这篇文章给大家介绍Nginx内置变量应用场景分析,感兴趣的朋友跟随小编一... 目录1. Nginx 内置变量速查表2. 核心变量详解与应用场景3. 实际应用举例4. 注意事项Ng

前端Visual Studio Code安装配置教程之下载、汉化、常用组件及基本操作

《前端VisualStudioCode安装配置教程之下载、汉化、常用组件及基本操作》VisualStudioCode是微软推出的一个强大的代码编辑器,功能强大,操作简单便捷,还有着良好的用户界面,... 目录一、Visual Studio Code下载二、汉化三、常用组件1、Auto Rename Tag2