Struts2(一)基本配置
一、Struts2概述
1、什么是Struts2?
Struts2以WebWork为核心,采用拦截器的机制来处理用户的请求,这样使得业务逻辑控制器能够和ServletAPI脱离开来。
2、工作原理
当web容器接收到HttpServletRequest请求后,容器通过web.xml映射请求,并获得控制器的名字,然后容器调用控制器(StrutsPrepareAndExecuteFilter),控制器通过ActionMapper获取Action的信息并调用ActionProxy,ActionProxy读取struts.xml文件获取action和拦截器栈的信息,ActionProxy把请求传递给ActionInvocation,由ActionInvocation依次调用action和interceptor,根据action的配置信息,产生result,result信息返回给ActionInvocation并产生一个HttpServletResponse响应,将响应发送给客户端。
上述的步骤可由下图表示:

二、配置环境的基本步骤
1、在Eclipse中新建一个动态web工程。首先配置其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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>day_0818</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>
2、向工程中导入满足struts2开发的基本jar包

3、在src目录下配置struts.xml文件,其中的字段有如下:
package:包,struts2使用package来组织模块
name属性:必须,用于其它的用应用当前包
extends:当前继承哪个包,继承的,既可以继承其中的所有的配置,通常情况下继承struts-default, struts-default这个包在struts-default.xml文件中定义
namespace:可选,如果没有给出,默认为“/”,若namespace有一个非默认值,则要想调用这个包里的Action,就必须把这个属性所定义的命名空间添加到有关的URI字符串里
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="index">
<result>/index.jsp</result>
</action>
</package>
<include file="example.xml"/>
<!-- Add packages here -->
</struts>
4、在服务器上运行该工程,即可显示index.jsp页面内容

三、注意事项
1、具体视图的返回可以由用户自己定义的Action来决定,具体的手段是根据返回的字符串找到对应的配置项,来决定视图的内容,具体的Action实现可以是一个普通的Java类,里面有public String execute方法即可或者实现Action接口,最常用的是从ActionSupport继承,好处在于可以直接使用Struts2封装好的方法。另外,Action执行的时候并一定是要执行execute方法,可以在配置文件中配置Action的时候用method=来指定执行哪个方法,也可以在url地址中动态指定(DMI,推荐使用)。
Struts2(一)基本配置的更多相关文章
- Struts2 拦截器配置以及实现
@(Java ThirdParty)[Struts|Interceptor] Struts2 拦截器配置以及实现 Struts2的拦截器应用于Action,可以在执行Action的方法之前,之后或者两 ...
- struts2的注解配置全面解析
以前在用struts2的注解配置时总是要在web.xml中配置一个初始化参数(actionPackages),最近发现不灵了,仔细研究了下发现即使不用在web.xml中配置也能成功,但时灵时不灵的,很 ...
- Struts2的通配符配置方式
Struts2的Action类很有意思,你可以使用3种方式来实现具体的Action类: 让你的Action类继承自ActionSupport类(项目中最常用这种方式,因为ActionSupport类中 ...
- Struts2之环境配置
在学习struts2之前,首先我们要明白使用struts2的目的是什么?它能给我们带来什么样的好处? 设计目标 Struts设计的第一目标就是使MVC模式应用于web程序设计.在这儿MVC模式的好处就 ...
- Struts2(四)Struts2配置文件的配置
Struts2的常见配置 1.Struts2的配置文件的加载顺序: 每次从客户端发送到请求到服务器都要先从Struts2的核心过滤器StrutsPrepareAndExeccuteFilter,这个过 ...
- struts2学习笔记(三)—— struts2的常见配置
一.配置文件的加载顺序 每次从客户端发送请求到服务器都要先经过Struts2的核心过滤器StrutsPrepareAndExecuteFilter,这个过滤器有两个功能:预处理和执行.在预处理中主要就 ...
- Struts2 第四讲 -- Struts2的基本配置
5.struts2的基本配置 5.1 struts2的访问连接url 在struts1中,通过<action path=“/primer/helloWorldAction.action”> ...
- JavaWeb框架_Struts2_(二)----->Struts2的核心配置
2. Struts2的核心配置 2.1 配置Struts.xml文件 2.1.1 Struts.xml文件 Struts2框架的核心配置文件是Struts.xml,该文件主要用来配置Action和 ...
- 3 Struts2的常见配置解析
1 package标签的相关配置 package标签:包,与Java中的包概念不一致.旨在更好的管理actionpackage标签的属性: name : 包的名称,在一个项目不重名即可,无具体含义 ...
- Struts2入门(二)——配置拦截器
一.前言 之前便了解过,Struts 2的核心控制器是一个Filter过滤器,负责拦截所有的用户请求,当用户请求发送过来时,会去检测struts.xml是否存在这个action,如果存在,服务器便会自 ...
随机推荐
- Sql Server性能优化辅助指标 - SET STATISTICS TIME ON和SET STATISTICS IO ON
1.前言 对于优化SQL语句或存储过程,以前主要是用如下语句来判断具体执行时间,但是SQL环境是复杂多变的,下面语句并不能精准判断性能是否提高:如果需要精确知道CPU.IO等信息,就无能为力了. ), ...
- Linux中./configure、make、make install详解
./configure && make && make install详解 2010-08-03 23:30:05 标签:休闲 ./configure &&a ...
- C# 把string字符导出到txt文档方法
public static string writtxt(string html, string file) { FileStream fileStream = new FileStream(Envi ...
- PostgreSQL安装入门教程
一.安装 首先,安装PostgreSQL客户端. sudo apt-get install postgresql-client 然后,安装PostgreSQL服务器. sudo apt-get ins ...
- HttpURLConnection如何添加请求头?
1.conn.setRequestProPerty(name,value),两个参数都是字符串.... 2.用httpURLConnection的setRequestProPerty(name,val ...
- UIView - CAGradientLayer
CAGradientLayer *layer = [[CAGradientLayer alloc] init]; layer.frame = self.bounds; //渐变转折点 layer.lo ...
- [SQLite3]connection string的连接池参数引发的错误
最近在.net中使用Sqlite数据库,发现.net的驱动做得不错,而且实现了加密功能.于是想给自己的数据库加上口令,结果,多次实验都以失败告终: 链接数据库,然后ChangePassword都成功执 ...
- 使用 resizableImageWithCapInsets 方法实现可伸缩图片
之前介绍过通过 stretchableImageWithLeftCapWidth:topCapHeight: 方法来实现可伸缩图片: 可看这篇随笔:使用 stretchableImageWithLef ...
- vue获取dom元素注意问题
mounted(){ setTimeout(()=>{ this.contentToggle(); },1000) }, methods:{ contentToggle(){ console.l ...
- WebJars are client-side web libraries (e.g. jQuery & Bootstrap) packaged into JAR (Java Archive) files
webjars网站https://www.webjars.org/,这里将很多的东西都打包成了jar包,想要用什么只需要导入相关的依赖就可以了. 比如springboot会用到jquery,webja ...