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

相关文章

史上最全nginx详细参数配置

《史上最全nginx详细参数配置》Nginx是一个轻量级高性能的HTTP和反向代理服务器,同时也是一个通用代理服务器(TCP/UDP/IMAP/POP3/SMTP),最初由俄罗斯人IgorSyso... 目录基本命令默认配置搭建站点根据文件类型设置过期时间禁止文件缓存防盗链静态文件压缩指定定错误页面跨域问题

nginx负载均衡及详细配置方法

《nginx负载均衡及详细配置方法》Nginx作为一种高效的Web服务器和反向代理服务器,广泛应用于网站的负载均衡中,:本文主要介绍nginx负载均衡及详细配置,需要的朋友可以参考下... 目录一、 nginx负载均衡策略1.1 基本负载均衡策略1.2 第三方策略1.3 策略对比二、 nginx配置2.1

使用Python开发Markdown兼容公式格式转换工具

《使用Python开发Markdown兼容公式格式转换工具》在技术写作中我们经常遇到公式格式问题,例如MathML无法显示,LaTeX格式错乱等,所以本文我们将使用Python开发Markdown兼容... 目录一、工具背景二、环境配置(Windows 10/11)1. 创建conda环境2. 获取XSLT

SpringBoot中四种AOP实战应用场景及代码实现

《SpringBoot中四种AOP实战应用场景及代码实现》面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护,在SpringBoot应用中,AO... 目录引言场景一:日志记录与性能监控业务需求实现方案使用示例扩展:MDC实现请求跟踪场景二:权限控制与

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

SpringBoot3.4配置校验新特性的用法详解

《SpringBoot3.4配置校验新特性的用法详解》SpringBoot3.4对配置校验支持进行了全面升级,这篇文章为大家详细介绍了一下它们的具体使用,文中的示例代码讲解详细,感兴趣的小伙伴可以参考... 目录基本用法示例定义配置类配置 application.yml注入使用嵌套对象与集合元素深度校验开发

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖

C语言中位操作的实际应用举例

《C语言中位操作的实际应用举例》:本文主要介绍C语言中位操作的实际应用,总结了位操作的使用场景,并指出了需要注意的问题,如可读性、平台依赖性和溢出风险,文中通过代码介绍的非常详细,需要的朋友可以参... 目录1. 嵌入式系统与硬件寄存器操作2. 网络协议解析3. 图像处理与颜色编码4. 高效处理布尔标志集合

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服