访问HelloWorld应用的路径的设置

HelloWorldAction文件:

package cn.itcast.primer;import com.opensymphony.xwork2.ActionSupport;public class HelloWorldAction extends ActionSupport{public String execute() throws Exception {System.out.println("HelloWorldAction ************* execute()");return "success";}
}

struts.xml

 <package name="primer" namespace="/primer"   extends="struts-default"><action name="helloWorldAction" class="cn.itcast.primer.HelloWorldAction"><result name="success" type="dispatcher">/primer/success.jsp</result></action></package>

在struts2中,访问struts2中action的URL路径由两部份组成:
包的命名空间+action的名称
例如: 访问本例子HelloWorldAction的URL路径为: /primer/helloWorldAction.action (注意:完整路径为:http://localhost:端口/内容路径/primer/helloWorldAction.action)。另外我们也可以加上.action后缀访问此Action。

 <package name="primer" namespace=“/primer“   extends="struts-default"><action name="helloWorldAction" class="cn.itcast.primer.HelloWorldAction"><result name="success" type="dispatcher">/success.jsp</result></action></package>

指定多个struts配置文件

在大部分应用里,随着应用规模的增加,系统中Action的数量也会大量增加,导致struts.xml配置文件变得非常臃肿。为了避免struts.xml文件过于庞大、臃肿,提高struts.xml文件的可读性,我们可以将一个struts.xml配置文件分解成多个配置文件,然后在struts.xml文件中包含其他配置文件。下面的struts.xml通过元素指定多个配置文件:

<struts><include file="struts-user.xml"/><include file="struts-order.xml"/>
</struts>

通过这种方式,我们就可以将Struts 2的Action按模块添加在多个配置文件中。

struts.xml文件配置:

<?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><!-- 引入自定义配置文件 --><include file="cn/itcast/primer/struts_primer.xml"></include></struts>

自定义配置文件struts_primer.xml:

<?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><!-- /primer/helloWorldAction.actionpackage:包* name:包名,唯一的,必选项* namespace:命名空间,唯一的,相当于房间号。可选项,省略情况下是"/"。页面中请求连接的前半部分* extends:继承* extends="struts-default":struts2框架底层提供的核心包struts2-core-2.3.3.jar下的struts-default.xml文件* 为什么要继承这个struts-default.xml文件?* 因为struts2框架底层提供的struts-default.xml声明了所有的拦截器和拦截器栈,知道在struts2框架运行时执行struts-default.xml文件中的拦截器栈。* 如果不继承struts-default.xml文件,就没有办法使用struts2框架提供的所有拦截器--><package name="primer" namespace="/primer" extends="struts-default"><!-- 如果找不到对应的action名的时候,配置默认要执行的action * name:指定action的名称--><default-action-ref name="helloWorldAction"></default-action-ref><!-- action:* name:对应页面中请求连接的后面半部分* class:对应要执行的类的完整路径--><action name="helloWorldAction" class="cn.itcast.primer.HelloWorldAction"><!-- result:结果类型* name:对应的是执行的类的方法的返回值public String execute() throws Exception {System.out.println("HelloWorldAction ************* execute()");return "success";}* 后半部分的文本内容:要转向到的页面--><result name="success">/primer/success.jsp</result></action><!-- 没有为action指定class* 在struts2框架底层的struts-default.xml文件中,配置了默认执行的类com.opensymphony.xwork2.ActionSupportpublic String execute() throws Exception {return SUCCESS;}* 实际上,默认执行的是底层提供的ActionSupport类的execute()方法* result结果类型,默认是根据struts2框架底层提供的ActionSupport类的execute()方法返回值,进行跳转--><action name="actionNoClass"><result name="success">/primer/success.jsp</result></action></package></struts>

自定义配置文件需要放置在对应的包内,如图所示:

struts.xml文件配置中还需要引入自定义配置文件


Action名称的搜索顺序

test.jsp文件:

<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags"   prefix="s"%>
<html><head><title>My JSP 'index.jsp' starting page</title></head><body>入门的路径:<br>  <a href="${pageContext.request.contextPath}/primer/helloWorldAction.action">helloWorld</a><br>测试Action名称的搜索顺序:<br><a href="${pageContext.request.contextPath}/primer/primer/aaa/primer/helloWorldAction.action">helloWorld</a><br><a href="${pageContext.request.contextPath}/primer/primer/helloWorldAction.action">helloWorld</a><br><a href="${pageContext.request.contextPath}/primer/helloWorldAction.action">helloWorld</a><br>没有为action指定class<br><a href="${pageContext.request.contextPath}/primer/actionNoClass.action">helloWorld</a><br>测试struts2 输出没有命名空间helloworld:<br><a href="${pageContext.request.contextPath}/primer/userAction.action">helloWorld</a><br></body>
</html>

  1. 测试第一个超链接
  2. 测试第二个超链接
  3. 测试第三个超链接
  4. 测试第四个超链接
  5. 测试第五个超链接
  6. 测试第六个超链接

总结:
1.获得请求路径的URI,例如url是:
http://server/struts2/path1/path2/path3/test.action

2.首先寻找namespace为/path1/path2/path3的package,
如果存在这个package,则在这个package中寻找名字为test的action,
如果不存在这个package则转步骤3;

3.寻找namespace为/path1/path2的package,
如果存在这个package,则在这个package中寻找名字为test的action,
如果不存在这个package,则转步骤4;

4.寻找namespace为/path1的package,
如果存在这个package,则在这个package中寻找名字为test的action,
如果仍然不存在这个package,就去默认的namaspace的package下面去找名
字为test的action(默认的命名空间为空字符串“/” ),
如果还是找不到,页面提示找不到action。


Action配置中的各项默认值

问题如果没有为action指定class,默认是com.opensymphony.xwork2.ActionSupport执行ActionSupport中的execute方法 ,由struts-default.xml文件<default-class-ref class="com.opensymphony.xwork2.ActionSupport" />决定

<package name="primer" namespace="/primer" extends="struts-default"><!-- 没有为action指定class* 在struts2框架底层的struts-default.xml文件中,配置了默认执行的类com.opensymphony.xwork2.ActionSupportpublic String execute() throws Exception {return SUCCESS;}* 实际上,默认执行的是底层提供的ActionSupport类的execute()方法* result结果类型,默认是根据struts2框架底层提供的ActionSupport类的execute()方法返回值,进行跳转--><action name="actionNoClass"><result name="success">/primer/success.jsp</result></action>
</package>
  1. 如果没有为action指定class,默认是ActionSupport。
  2. 如果没有为action指定method,默认执行action中的execute() 方法。ActionSupport的execute方法里面就一句话return “success”;
  3. 如果没有指定result的name属性,默认值为success。
    问题:如果请求的路径查找不到action的情况下,程序运行会抛出异常 ,可以通过配置当找不到action的情况下,会执行默认的action
<package name="primer" namespace="/primer" extends="struts-default">
<!-- 如果找不到对应的action名的时候,配置默认要执行的action * name:指定action的名称
-->
<default-action-ref name="helloWorldAction"></default-action-ref></package>

ActionSupport

ActionSupport类是默认的 Action 类. 在编写 Action 类时, 通常会对这个类进行扩展


Struts 2处理的请求后缀

StrutsPrepareAndExecuteFilter是Struts 2框架的核心控制器,它负责拦截由<url-pattern>/*</url-pattern>指定的所有用户请求,当用户请求到达时,该Filter会过滤用户的请求。默认情况下,如果用户请求的路径不带后缀或者后缀以.action结尾,这时请求将被转入Struts 2框架处理,否则Struts 2框架将略过该请求的处理。

根据配置文件:struts2-core-2.3.31.jar包下的 org.apache.struts2/default.properties文件定义的常量决定
struts.action.extension=action,,

默认处理的后缀是可以通过常量”struts.action.extension“进行修改的,如下面配置Struts 2只处理以.do为后缀的请求路径:

<struts><constant name="struts.action.extension" value="do"/>
</struts>

struts.xml文件:

<?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:指定的是struts2框架底层提供的default.properties资源文件中配置的"常量"* value:指定的是配置常量的值* 在struts.xml文件中,配置的常量的值会覆盖底层提供的default.properties资源文件中配置的常量的值* 配置struts2框架的页面中请求连接的后缀名,如果指定多个的话,用","隔开* 如果在struts.xml中和struts.properties资源文件中同时进行配置,struts.properties的配置起作用* 因为常量可以在多个配置文件中进行定义,所以我们需要了解下struts2加载常量的搜索顺序:1 struts-default.xml2 struts-plugin.xml3 struts.xml4 struts.properties(自己创建)5 web.xml--><!-- <constant name="struts.action.extension" value="do,love"></constant> --><!-- 引入自定义配置文件 --><include file="cn/itcast/primer/struts_primer.xml"></include></struts>

细说常量定义

常量可以在struts.xml或struts.properties中配置,建议在struts.xml中配置,两种配置方式如下:
1.在struts.xml文件中配置常量

<struts><constant name="struts.action.extension" value="do"/>
</struts>

2.在struts.properties中配置常量, (struts.properties文件放置在src下)

struts.action.extension=do

因为常量可以在多个配置文件中进行定义,所以我们需要了解下struts2加载常量的搜索顺序:
1 struts-default.xml
2 struts-plugin.xml
3 struts.xml
4 struts.properties
5 web.xml
如果在多个文件中配置了同一个常量,则后一个文件中配置的常量值会覆盖前面文件中配置的常量值.


常用的常量介绍

  • 指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法 和freemarker 、velocity的输出
    <constant name="struts.i18n.encoding" value="UTF-8"/>

  • 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
    如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开 <constant name="struts.action.extension" value="do"/>

  • 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭
    <constant name="struts.serve.static.browserCache" value="false"/>
  • 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好
    <constant name="struts.configuration.xml.reload" value="true"/>

  • 开发模式下使用,这样可以打印出更详细的错误
    <constant name="struts.devMode" value="true" />

  • 默认的视图
    <constant name="struts.ui.theme" value="simple" />

  • 与spring集成时,指定由spring负责action对象的
    <constant name="struts.objectFactory" value="spring" />

  • 该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为 false
    <constant name="struts.enable.DynamicMethodInvocation" value="false"/>

  • 上传文件的大小限制
    <constant name="struts.multipart.maxSize" value=“10701096"/>

    struts.xml文件配置:

<?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:指定的是struts2框架底层提供的default.properties资源文件中配置的"常量"* value:指定的是配置常量的值* 在struts.xml文件中,配置的常量的值会覆盖底层提供的default.properties资源文件中配置的常量的值* 配置struts2框架的页面中请求连接的后缀名,如果指定多个的话,用","隔开* 如果在struts.xml中和struts.properties资源文件中同时进行配置,struts.properties的配置起作用* 因为常量可以在多个配置文件中进行定义,所以我们需要了解下struts2加载常量的搜索顺序:1 struts-default.xml2 struts-plugin.xml3 struts.xml4 struts.properties(自己创建)5 web.xml--><!-- <constant name="struts.action.extension" value="do,love"></constant> --><!-- 配置国际化资源文件修改时,是否重新加载。默认是false为不加载,true是加载 --><!-- <constant name="struts.i18n.reload" value="true"></constant> --><!-- 配置struts2框架的配置文件修改时,是否重新加载。默认是false为不加载,true是加载 --><!-- <constant name="struts.configuration.xml.reload" value="true"></constant> --><!-- 配置struts2框架的模式* 默认值是false,是生产模式* true是开发模式,需要更多的调试信息### includes:### - struts.i18n.reload = true### - struts.configuration.xml.reload = true--><constant name="struts.devMode" value="true"></constant><!-- 配置动态方法调用,设置成不开启。默认是true是开启状态;false是不开启状态 --><constant name="struts.enable.DynamicMethodInvocation" value="false"></constant><!-- 配置所有资源文件,省略后缀名,如果配置多个资源文件时,用","隔开。不仅是国际化资源文件* 类型转换器的错误提示资源文件--><constant name="struts.custom.i18n.resources" value="cn.itcast.converter.converter,cn.itcast.i18n.resources"></constant><!-- 配置文件上传的总大小 --><constant name="struts.multipart.maxSize" value="2097152000"></constant><!-- 引入自定义配置文件 --><include file="cn/itcast/primer/struts_primer.xml"></include></struts>

Struts2基本配置相关推荐

  1. [JavaWeb基础] 007.Struts2的配置和简单使用

    1.框架简介 采用Struts能开发出基于MVC(Model-View-Controller)设计模式的应用构架,用于快速开发Java Web应用.Struts实现的重点在C(Controller), ...

  2. Struts2 XML配置详解

    2019独角兽企业重金招聘Python工程师标准>>> 1.    深入Struts2的配置文件 本部分主要介绍struts.xml的常用配置. 1.1.    包配置: Strut ...

  3. Struts2中配置默认Action

    Struts2中配置默认Action 一.jsp默认设置 1.当访问的Action不存在时,页面会显示错误信息,可以通过配置默认Action处理用户异常的操作: 2.配置方法: 在struts.xml ...

  4. Struts2 Convention Plugin ( struts2 零配置 )

    Struts2 Convention Plugin ( struts2 零配置 ) convention-plugin 可以用来实现 struts2 的零配置. 零配置的意思并不是说没有配置,而是通过 ...

  5. struts2关键配置及函数总结,

    一.关键配置总结(关键点已加粗): 导入min-lib 1.web.xml配置(官方文档标准配置): struts2过滤器: <!-- struts2框架配置1 --><!-- st ...

  6. Struts2 xml配置

    1. 深入Struts2的配置文件 本部分主要介绍struts.xml的常用配置. 1.1. 包配置: Struts2框架中核心组件就是Action.拦截器等,Struts2框架使用包来管理Actio ...

  7. Struts2零配置属性详解(2)

    2019独角兽企业重金招聘Python工程师标准>>> Struts2 零配置属性详解 一.插件包 直接引入myEclipse里面的struts code 包即可. struts2- ...

  8. struts2路径配置_Struts 2结果路径配置示例

    struts2路径配置 This is the third post in the Struts 2 series. You might want to check out earlier posts ...

  9. Struts2之配置使用

    重要声明:此次学习struts2使用的版本号为:struts-2.3.15.3.假设是用的其它版本号出现的问题能够联系我. 一. 1.首先就是打开myeclipse创建project名为:struts ...

  10. struts2的配置

    1,建立javaee5的web project:在lib包中放入以下几个包 形成的工程结构图如下所示: 2,创建视图页面login.jsp  success.jsp error.jsp 其中login ...

最新文章

  1. 特征工程的宝典-《Feature Engineering for Machine Learning》翻译及代码实现
  2. 30.Node.js 全局对象
  3. Redis 购物车 - 删除商品与更新购买数量
  4. mysql安装8.013_Mysql 8.0.13 安装
  5. JavaScript tab页
  6. eclipse自动补全设置
  7. Linux入门(3)_Linux常用命令(待完善)
  8. Windows系统安装教程
  9. 【Leetcode】【Regular Expression Matching】【正则表达式匹配】【C++】
  10. 软件工程个人作业01 100以内四则运算自动答题系统(含整数和真分数)
  11. 推荐一款好用的取色器(仅支持windows)
  12. CodeMeter服务不能启动的解决方法,rslogix5000,无法启动,codemeter服务没有启动,
  13. 锐浪报表开发Web版
  14. win10桌面显示计算机及网上邻居,Win10网上邻居在哪? Win10桌面显示网上邻居网络图标方法...
  15. 邮箱收取后删除服务器邮件设置,邮件为什么会被客户端(POP)收取并删除?能不能避免这种情况?...
  16. java学习笔记----Mybatis-Plus
  17. Stream Collectors - filtering
  18. 新磁盘分区格式化挂载
  19. 关注ERP项目中的隐含成本
  20. 数据结构与算法A实验六图论---7-3 旅游规划(Dijkstra算法)

热门文章

  1. 今天仔细看了看struts方面的英文文档
  2. Android与Ios简单有效的防止wifi劫持与各种抓包
  3. 迭代与递归的区别(斐波那契数列和小猴子摘桃)
  4. 【首次送书福利】html+css+js制作动态千纸鹤
  5. linux修改分辨率的命令,linux更改分辨率命令行
  6. 深度挖掘最近很火的“飞滴出行项目”,究竟是好是坏?
  7. ALEXNET 论文总结
  8. 手机镜头参数--手机摄影训练营第五期---S05-20170605
  9. OG 488, acid|195136-52-8|Oregon Green 488 Carboxylic Acid
  10. 微软原生输入法的一些功能和技巧