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

相关文章

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

Java MQTT实战应用

《JavaMQTT实战应用》本文详解MQTT协议,涵盖其发布/订阅机制、低功耗高效特性、三种服务质量等级(QoS0/1/2),以及客户端、代理、主题的核心概念,最后提供Linux部署教程、Sprin... 目录一、MQTT协议二、MQTT优点三、三种服务质量等级四、客户端、代理、主题1. 客户端(Clien

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

SpringBoot开发中十大常见陷阱深度解析与避坑指南

《SpringBoot开发中十大常见陷阱深度解析与避坑指南》在SpringBoot的开发过程中,即使是经验丰富的开发者也难免会遇到各种棘手的问题,本文将针对SpringBoot开发中十大常见的“坑... 目录引言一、配置总出错?是不是同时用了.properties和.yml?二、换个位置配置就失效?搞清楚加

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信

Python中对FFmpeg封装开发库FFmpy详解

《Python中对FFmpeg封装开发库FFmpy详解》:本文主要介绍Python中对FFmpeg封装开发库FFmpy,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、FFmpy简介与安装1.1 FFmpy概述1.2 安装方法二、FFmpy核心类与方法2.1 FF

Nginx 重写与重定向配置方法

《Nginx重写与重定向配置方法》Nginx重写与重定向区别:重写修改路径(客户端无感知),重定向跳转新URL(客户端感知),try_files检查文件/目录存在性,return301直接返回永久重... 目录一.try_files指令二.return指令三.rewrite指令区分重写与重定向重写: 请求