Struts2


layout: post
title: Struts2
subtitle: Struts基础
date: 2018-05-28
author: ZL
header-img: img/20180528.jpg
catalog: true
tags:
- Struts


搭建运行实例

  1. 导包
6262743-f10068daf9ea9c81.png
image
  1. 书写Action类
package zl.hello;
public class TestAction {
    public String test() {
        // TODO Auto-generated method stub
        System.out.println("1111111111111111111");
        return "success";
    }
}
  1. 书写src/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>
    <package name="test" namespace="/test" extends="struts-default">
        <action name="TestAction" class="zl.hello.TestAction" method="test">
            <result name="success">/hello.jsp</result>
        </action>
    </package>
</struts>
  1. web.xml
<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>
  1. 运行
    6262743-0b0547c34a1e7ddc.png
    image

配置文件详解

struts.xml配置

  • package:将Action配置封装.就是可以在Package中配置很多action.
  • package的name:package的名字,不能与其他package name重复。
  • package的namespace:给action的访问路径中定义一个命名空间。就是在浏览器里面输入的地址。
  • package的extends:继承一个指定包。
  • package的abstract(一般用不到):包是否为抽象的,标识性属性,标识该包不能独立运行,专门用来被继承。
  • action的name:浏览器中的地址名称。
  • action的class:Action的全类名。
  • action的method:Action类的方法名称。指定调用Action的哪个方法来处理请求。
  • result的name:action中方法的返回值。标签体的内容是页面的路径。
  • result的type:指定调用哪个result类来处理结果。默认type="dispatcher"。

6262743-505c124384008130.png
image

result的type有下面几种。
6262743-d2aed09971f9e698.png
image

struts2常量配置

不去配置的话,有默认的配置。

6262743-0691ff6260273e67.png
image

修改常量配置。(一般就使用第一种)

  • 方式一:
    在src/struts.xml中:
    6262743-8f1c690c9acae38c.png
    image
  • 方式二:
    在src下创建struts.properties
    6262743-c89f0fc707012eb3.png
    image
  • 方式三:
    在项目的web.xml中
    6262743-13270f66174c3c46.png
    image

加载顺序(后加载的配置覆盖先加载的配置)

6262743-bb1bc9666ff97f66.png
image

常量有哪些?

<!-- i18n:国际化. 解决post提交乱码 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!-- 指定反问action时的后缀名(默认的后缀名是.action或者空) -->
<constant name="struts.action.extension" value="action"></constant>

上面的value值如果是action的话,浏览器的地址最后.action 或者为空。http://localhost:8080/struts2_day01/hello/HelloAction.action  

但是如果是别的,比如value = “do”,那么浏览器的地址必须要.do。http://localhost:8080/struts2_day01/hello/HelloAction.do  
<!-- 指定struts2是否以开发模式运行,默认false  
    1.热加载主配置.(不需要重启即可生效)
    2.提供更多错误信息输出,方便开发时的调试
 -->
<constant name="struts.devMode" value="true"></constant>

开启以后,更改struts的配置内容,不需要重新启动服务器就可以生效。  
提供了更多的错误输出,方便开发调试。  
建议开发的时候打开,开发结束后关闭。  

进阶配置

动态方法调用

问题引入

还是上面的例子,现在TestAction里面有多个方法。

package zl.hello;

public class TestAction {

    public String fun1() {
        System.out.println("fun1");
        return "success";
    }
    
    public String fun2() {
        System.out.println("fun2");
        return "success";
    }
    
    public String fun3() {
        System.out.println("fun3");
        return "success";
    }
}

可是一个package只能配置一个method。想要调用三个方法,还得配置三个package。

<package name="test" namespace="/test" extends="struts-default">
  <action name="TestAction" class="zl.hello.TestAction" method="fun1">
    <result name="success">/hello.jsp</result>
  </action>
</package>

  <package name="test2" namespace="/test2" extends="struts-default">
  <action name="TestAction" class="zl.hello.TestAction" method="fun2">
    <result name="success">/hello.jsp</result>
  </action>
</package>

  <package name="test3" namespace="/test3" extends="struts-default">
  <action name="TestAction" class="zl.hello.TestAction" method="fun3">
    <result name="success">/hello.jsp</result>
  </action>
</package>

这就比较麻烦了,动态方法调用就可以解决这个问题,一个package就可以访问到Action里面的多个方法。

方式一(不常用)

strutsx.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="struts.enable.DynamicMethodInvocation" value="true"></constant>
   
   <package name="test" namespace="/test" extends="struts-default">
       <action name="TestAction" class="zl.hello.TestAction">
           <result name="success">/hello.jsp</result>
       </action>
   </package>
</struts>

使用:

调用fun1  
http://localhost:8080/struts1.1/test/TestAction!fun1
调用fun2  
http://localhost:8080/struts1.1/test/TestAction!fun2
调用fun3  
http://localhost:8080/struts1.1/test/TestAction!fun3

方式二

strutsx.xml配置文件.注意action的name和method。

<?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>
    
    <package name="test" namespace="/test" extends="struts-default">
        <action name="TestAction_*" class="zl.hello.TestAction" method="{1}">
            <result name="success">/hello.jsp</result>
        </action>
    </package>
</struts>

使用:

调用fun1  
http://localhost:8080/struts1.1/test/TestAction_fun1
调用fun2  
http://localhost:8080/struts1.1/test/TestAction_fun2
调用fun3  
http://localhost:8080/struts1.1/test/TestAction_fun3

include引入xml

与Android的xml里面的include引入功能一样。

6262743-75d26e824031525c.png
image

Action类的创建方式。(方式三最常用)

方式一

//方式1: 创建一个类.可以是POJO
//POJO:不用继承任何父类.也不需要实现任何接口.
//使struts2框架的代码侵入性更低.
public class Demo3Action {

}

方式二

//方式2: 实现一个接口Action
// 里面有execute方法,提供action方法的规范.
// Action接口预置了一些字符串.可以在返回结果时使用.为了方便
//Action.SUCCESS;
//Action.ERROR;
//Action.NONE;
//Action.INPUT;
//Action.LOGIN
import com.opensymphony.xwork2.Action;
public class Demo4Action implements Action {
    @Override
    public String execute() throws Exception {

        return Action.SUCCESS;
    }
}

方式三

//方式3: 继承一个类.ActionSupport
// 帮我们实现了 Validateable, ValidationAware, TextProvider, LocaleProvider .
//如果我们需要用到这些接口的实现时,不需要自己来实现了.
import com.opensymphony.xwork2.ActionSupport;
public class Demo5Action  extends ActionSupport{

}

result的type的四种类型

6262743-dc614119933558b0.png
image

type有很多种类型,这里介绍四个,他们是dispatcher、redirect、chain、redirectAction。

<package name="result" namespace="/" extends="struts-default" >
    <!--  转发 -->
        <action name="Demo1Action" class="cn.itheima.a_result.Demo1Action" method="execute" >
            <result name="success" type="dispatcher" >/hello.jsp</result>
        </action>
    <!-- 重定向 -->
        <action name="Demo2Action" class="cn.itheima.a_result.Demo2Action" method="execute" >
            <result name="success" type="redirect" >/hello.jsp</result>
        </action>
    <!-- 转发到Action -->
        <action name="Demo3Action" class="cn.itheima.a_result.Demo3Action" method="execute" >
             <result name="success" type="chain">
                    <!-- action的名字 -->
                 <param name="actionName">Demo1Action</param>
                    <!-- action所在的命名空间 -->
                 <param name="namespace">/</param>
             </result>
        </action>
        <!-- 重定向到Action -->
        <action name="Demo4Action" class="cn.itheima.a_result.Demo4Action" method="execute" >
            <result  name="success"  type="redirectAction">
                 <!-- action的名字 -->
                 <param name="actionName">Demo1Action</param>
                 <!-- action所在的命名空间 -->
                 <param name="namespace">/</param>
            </result>
        </action>
    </package>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值