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

相关文章

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

mybatis映射器配置小结

《mybatis映射器配置小结》本文详解MyBatis映射器配置,重点讲解字段映射的三种解决方案(别名、自动驼峰映射、resultMap),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定... 目录select中字段的映射问题使用SQL语句中的别名功能使用mapUnderscoreToCame

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

MySQL8 密码强度评估与配置详解

《MySQL8密码强度评估与配置详解》MySQL8默认启用密码强度插件,实施MEDIUM策略(长度8、含数字/字母/特殊字符),支持动态调整与配置文件设置,推荐使用STRONG策略并定期更新密码以提... 目录一、mysql 8 密码强度评估机制1.核心插件:validate_password2.密码策略级

ShardingProxy读写分离之原理、配置与实践过程

《ShardingProxy读写分离之原理、配置与实践过程》ShardingProxy是ApacheShardingSphere的数据库中间件,通过三层架构实现读写分离,解决高并发场景下数据库性能瓶... 目录一、ShardingProxy技术定位与读写分离核心价值1.1 技术定位1.2 读写分离核心价值二