【Java中级】15.0 SSH之Struts2框架(二)——package、action、常量和include的配置及 Action的编写和访问...

在struts2.xml的根标签下,可以对各种Action进行相关配置:


16102290-92da4d1a93eb6b7c.png
image.png

主要是package、action、include需要熟悉怎么配置,有什么作用。

1.0 package相关配置
  • package标签称为包,这个包与Java中的包的概念不一致。包为了更好管理action的配置。

package标签的属性

  • name :包的名称,只有在一个项目中不重名即可。
  • extends :继承哪个包,通常值为struts-default。
  • namespace :名称空间,与<action>标签中的name属性共同决定访问路径。
  • 16102290-10a5016823f05ff5.png
    image.png
  • 名称空间有三种写法:
  • 带名称的名称空间 :namespace=”/aaa”
  • 根名称空间 :namespance=”/”
  • 默认名称空间 :namespace=””
  • 但是多个action,会先找带名称的名称空间,然后找根名称空间,最后才会去找默认名称空间的namespace
  • abstract :抽象的,用于其他包的继承。(开发人员很少如此去定义)
2.0 action相关配置

action标签配置Action类。


16102290-689ba79700f93e18.png
image.png

action标签的属性

  • name :与namespace共同决定访问路径
  • class :Action类的全路径
  • method :执行Action中的哪个方法的方法名,默认值execute
  • converter :用于设置类型转换器(一般不用设置,struts内部提供的内部转换器足够开发人员使用)
3.0 常量的配置

Struts2的常量配置,在Struts2的框架中,提供了非常多的常量:(在default.properties

16102290-bd060c35dfad903f.png
image.png

  • struts.i18n.encoding=UTF-8 ----Struts2中所有的post请求的中文乱码不用处理。(get得处理)
  • struts.action.extension=action,, ----Struts2请求的默认的扩展名。默认扩展名是.action或者什么都不写。

在Struts2中修改一些常量的值:
修改常量的值,可以有三个位置进行修正。

1. 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">

<!-- START SNIPPET: -->
<struts>
    <!-- 配置struts2的常量 -->
    <!-- Struts2请求的默认的扩展名。value可设置任意扩展名 -->
    <constant name="struts.action.extension" value="action"/>

        …………

</struts>

<!-- END SNIPPET: -->
2. struts.properties中进行修改

在src目录下新建 struts.properties,但这个文件下只能修改常量。

struts.action.extension=action
3. 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">
        …………

    <!-- 配置struts2的核心过滤器 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <!-- 修改常量 -->
        <init-param>
            <param-name>struts.action.extension</param-name>
            <param-value>action</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
4.0 分模块开发的配置

include配置,引入其他路径下的配置文件。一般用于分模块开发(团队开发)和开发的整合。
新建一个struts_zhangsan.xml

16102290-e919161725370c72.png
image.png

struts_zhangsan.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">

<!-- START SNIPPET: -->
<struts>
    <!-- Struts2为了管理action的配置,通过包进行管理。 -->
    <!-- 配置Struts2的包========== -->
    <!-- name唯一,随便写 -->
    <!-- extends继承struts2-core-2.5.22.jar包下struts-default.xml的struts-default包 -->
    <package name="demo1" extends="struts-default" namespace="/">
        <!-- 配置Action========== -->
        <!--name: 和demo1.jsp的 <a href="${pageContext.request.contextPath }/hello.action">Struts2的入门</a>
            中的hello.action对应上,去掉.action -->
        <action name="hello" class="com.edp.struts.demo1.HelloAction">
            <!-- 配置页面的跳转========== -->
            <result name="success">/demo1/success.jsp</result>
        </action>
    </package>

</struts>

<!-- END SNIPPET: -->

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">

<!-- START SNIPPET: -->
<struts>
    <!-- 配置struts2的常量 -->
    <!-- Struts2请求的默认的扩展名。value可设置任意扩展名 -->
<!--    <constant name="struts.action.extension" value="action"/> -->
    
    <!-- 路径要com/edp/struts/demo1/xxx这样的格式 -->
    <include file="struts_zhangsan.xml"/>

</struts>

<!-- END SNIPPET: -->
5.0 Action的编写

修改和新建


16102290-2a47e0ab1f4a2ca4.png
image.png

struts_demo2.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">

<!-- START SNIPPET: -->
<struts>
    <!-- Struts2为了管理action的配置,通过包进行管理。 -->
    <!-- 配置Struts2的包========== -->
    <!-- name唯一,随便写 -->
    <!-- extends继承struts2-core-2.5.22.jar包下struts-default.xml的struts-default包 -->
    <package name="demo2" extends="struts-default" namespace="/">
        <!-- 配置Action========== -->
        <!--name: 和demo1.jsp的 <a href="${pageContext.request.contextPath }/hello.action">Struts2的入门</a>
            中的hello.action对应上,去掉.action -->
        <action name="actiondemo1" class="com.edp.struts.demo2.ActionDemo1"/>
        <action name="actiondemo2" class="com.edp.struts.demo2.ActionDemo2"/>
        <action name="actiondemo3" class="com.edp.struts.demo2.ActionDemo3"/>
    </package>

</struts>

<!-- END SNIPPET: -->
5.1Action类是POJO的类

POJO类,简单的java对象。本来应该叫Javabean,但是有语义冲突,再取了另一个名字。
ActionDemo1.java

package com.edp.struts.demo2;
/**
 * 
 * @Title: ActionDemo1.java
 * @Package com.edp.struts.demo2
 * @author EdPeng
 * @version 创建时间 2020年2月14日上午1:52:42
 * @Description Action的编写方式1:Action类是POJO的类
 * @version V1.0
 */
public class ActionDemo1 {
    /**
     * 提供一个方法
     * 方法签名固定的,公有的。返回值是string类型,方法名是execute 在这个方法中不能传递参数。
     * 
     */
    public String execute() {
        System.out.println("HelloAction执行了……");
        return null;
    }
}
5.2 Action类实现一个Action的接口

ActionDemo2.java

package com.edp.struts.demo2;

import com.opensymphony.xwork2.Action;

/**
 * 
 * @Title: ActionDemo2.java
 * @Package com.edp.struts.demo2
 * @author EdPeng
 * @version 创建时间 2020年2月14日上午2:01:11
 * @Description Action的编写方式2:Action类实现一个Action的接口
 * 实现接口的方式:提供5个常量(5个逻辑视图的名称)
 *      * SUCCESS = "success";  成功(可自定义)
 *      *  NONE = "none";  不跳转(可自定义)
 *      *  ERROR = "error";  失败(可自定义)
 *      *  INPUT = "input";  表单校验出错或者类型转换出错。(不能私自修改)
 *      *  LOGIN = "login"; 登录出错的页面的跳转(可自定义)
 * @version V1.0
 */
public class ActionDemo2 implements Action{

    @Override
    public String execute() throws Exception {
        System.out.println("ActionDemo2执行了……");
        return null;
        //等同于return NONE;
    }

}
5.3 Action类继承ActionSupport类(推荐使用)

ActionDemo3.java

package com.edp.struts.demo2;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @Title: ActionDemo3.java
 * @Package com.edp.struts.demo2
 * @author EdPeng
 * @version 创建时间 2020年2月14日上午2:08:58
 * @Description Action的编写方式3:Action类继承ActionSupport类
 *              推荐使用ActionSupport类
 *              提供了数据校验,国际化等一系列操作的方法。
  *              什么叫国际化:国内现在基本不用了。一台服务器一套代码在不同的国家都能用。目前国内的项目大都没有输出的机会
 *              一台服务器也不现实,基本上会在每个地方都有一台服务器。
 * @version V1.0
 */
public class ActionDemo3 extends ActionSupport {

    @Override
    public String execute() throws Exception {
        System.out.println("ActionDemo3执行了……");

        // 默认返回super.execute();
        return NONE;
    }
}
6.0 Struts2的Action的访问
16102290-c09a19b443c06757.png
image.png
6.1 通过method设置(比较low)

在struts.xml增加一行

<include file="com/edp/struts/demo3/struts_demo3.xml"/>

struts_demo3.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">

<!-- START SNIPPET: -->
<struts>
    <!-- Struts2为了管理action的配置,通过包进行管理。 -->
    <!-- 配置Struts2的包========== -->
    <!-- name唯一,随便写 -->
    <!-- extends继承struts2-core-2.5.22.jar包下struts-default.xml的struts-default包 -->
    <package name="demo3" extends="struts-default" namespace="/">
        <!-- 配置Action========== -->
        <!--name: 和demo1.jsp的 <a href="${pageContext.request.contextPath }/hello.action">Struts2的入门</a>
            中的hello.action对应上,去掉.action -->
        <action name="userFind" class="com.edp.struts.demo3.UserAction" method="find"/>
        <action name="userUpdate" class="com.edp.struts.demo3.UserAction" method="update"/>
        <action name="userDelete" class="com.edp.struts.demo3.UserAction" method="delete"/>
        <action name="userSave" class="com.edp.struts.demo3.UserAction" method="save"/>
    </package>

</struts>

<!-- END SNIPPET: -->

demo1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>Action的访问</h1>
    <h3>通过method方式</h3>
    <a href="${ pageContext.request.contextPath }/userFind.action">查询用户</a><br/>
    <a href="${ pageContext.request.contextPath }/userUpdate.action">修改用户</a><br/>
    <a href="${ pageContext.request.contextPath }/userDelete.action">删除用户</a><br/>
    <a href="${ pageContext.request.contextPath }/userSave.action">保存用户</a><br/>
</body>
</html>

UserAction.java

package com.edp.struts.demo3;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends  ActionSupport{

    public String find() {
        System.out.println("查询用户……");
        return NONE;
    }

    public String update() {
        System.out.println("修改用户……");
        return NONE;
    }

    public String delete() {
        System.out.println("删除用户……");
        return NONE;
    }

    public String save() {
        System.out.println("保存用户……");
        return NONE;
    }
}
6.2 通过通配符的方式进行配置(常用)

在demo1.jsp中

    <h3>通过通配符的方式</h3>
    <a href="${ pageContext.request.contextPath }/product_find.action">查询商品</a><br/>
    <a href="${ pageContext.request.contextPath }/product_update.action">修改商品</a><br/>
    <a href="${ pageContext.request.contextPath }/product_delete.action">删除商品</a><br/>
    <a href="${ pageContext.request.contextPath }/product_save.action">保存商品</a><br/>

在struts_demo3.xml中:

        <!-- 通配符的方式 -->
        <!-- *表示通配,后面的1表示第一个* -->
        <action name="product_*" class="com.edp.struts.demo3.ProductAction" method="{1}" />

新建一个ProductAction.java类:

package com.edp.struts.demo3;

import com.opensymphony.xwork2.ActionSupport;

public class ProductAction extends ActionSupport {

    public String find() {
        System.out.println("查询商品……");
        return NONE;
    }

    public String update() {
        System.out.println("修改商品……");
        return NONE;
    }

    public String delete() {
        System.out.println("删除商品……");
        return NONE;
    }

    public String save() {
        System.out.println("保存商品……");
        return NONE;
    }
}

更抽象的写法:
demo1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
        <h1>Action的访问</h1>
    <h3>通过method方式</h3>
    <a href="${ pageContext.request.contextPath }/UserAction_find.action">查询用户</a><br/>
    <a href="${ pageContext.request.contextPath }/UserAction_update.action">修改用户</a><br/>
    <a href="${ pageContext.request.contextPath }/UserAction_delete.action">删除用户</a><br/>
    <a href="${ pageContext.request.contextPath }/UserAction_save.action">保存用户</a><br/>
    
    <h3>通过通配符的方式</h3>
    <a href="${ pageContext.request.contextPath }/ProductAction_find.action">查询商品</a><br/>
    <a href="${ pageContext.request.contextPath }/ProductAction_update.action">修改商品</a><br/>
    <a href="${ pageContext.request.contextPath }/ProductAction_delete.action">删除商品</a><br/>
    <a href="${ pageContext.request.contextPath }/ProductAction_save.action">保存商品</a><br/>
</body>
</html>

struts_demo3.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">

<!-- START SNIPPET: -->
<struts>
    <!-- Struts2为了管理action的配置,通过包进行管理。 -->
    <!-- 配置Struts2的包========== -->
    <!-- name唯一,随便写 -->
    <!-- extends继承struts2-core-2.5.22.jar包下struts-default.xml的struts-default包 -->
    <package name="demo3" extends="struts-default" namespace="/">
        <!-- 配置Action========== -->
        <!-- 通配符的方式 -->
        <!-- *表示通配,后面的1表示第一个* -->
        <!-- <action name="product_*" class="com.edp.struts.demo3.ProductAction" method="{1}" /> -->
        <!-- 通配符更抽象的写法 -->
        <action name="*_*" class="com.edp.struts.demo3.{1}" method="{2}" />
    </package>

</struts>

<!-- END SNIPPET: -->
6.3 动态方法访问

此方式默认不开启。


16102290-91787aabf57b3fed.png
image.png

需要手动在struts.xml中设置开启。
我这里在struts_demo3.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">

<!-- START SNIPPET: -->
<struts>
<!-- 开启动态方法访问 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>

        …………

</struts>

<!-- END SNIPPET: -->

struts_demo3.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">

<!-- START SNIPPET: -->
<struts>
<!-- 开启动态方法访问 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
    <!-- Struts2为了管理action的配置,通过包进行管理。 -->
    <!-- 配置Struts2的包========== -->
    <!-- name唯一,随便写 -->
    <!-- extends继承struts2-core-2.5.22.jar包下struts-default.xml的struts-default包 -->
    <package name="demo3" extends="struts-default" namespace="/">
        <!-- 配置Action========== -->
        <!--name: 和demo1.jsp的 <a href="${pageContext.request.contextPath }/hello.action">Struts2的入门</a>
            中的hello.action对应上,去掉.action -->
        <!-- <action name="userFind" class="com.edp.struts.demo3.UserAction" method="find" /> -->
        <!-- <action name="userUpdate" class="com.edp.struts.demo3.UserAction" method="update" /> -->
        <!-- <action name="userDelete" class="com.edp.struts.demo3.UserAction" method="delete" /> -->
        <!-- <action name="userSave" class="com.edp.struts.demo3.UserAction" method="save" /> -->

        <!-- 通配符的方式 -->
        <!-- *表示通配,后面的1表示第一个* -->
        <!-- <action name="product_*" class="com.edp.struts.demo3.ProductAction" method="{1}" /> -->
        <!-- 通配符更抽象的写法 -->
        <action name="*_*" class="com.edp.struts.demo3.{1}" method="{2}" />

        <!-- 动态方法访问的方式 -->
        <!-- 默认关闭,需要手动打开 -->
        <action name="customer" class="com.edp.struts.demo3.CustomerAction" />
    </package>

</struts>

<!-- END SNIPPET: -->

新建CustomerAction.java

package com.edp.struts.demo3;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @Title: CustomerAction.java
 * @Package com.edp.struts.demo3
 * @author EdPeng
 * @version 创建时间 2020年2月14日上午10:43:57
 * @Description 动态方法访问
 * @version V1.0
 */
public class CustomerAction extends ActionSupport{
    public String find() {
        System.out.println("查询客户……");
        return NONE;
    }

    public String update() {
        System.out.println("修改客户……");
        return NONE;
    }

    public String delete() {
        System.out.println("删除客户……");
        return NONE;
    }

    public String save() {
        System.out.println("保存客户……");
        return NONE;
    }
}

demo1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
        <h1>Action的访问</h1>
    <h3>通过method方式</h3>
    <a href="${ pageContext.request.contextPath }/UserAction_find.action">查询用户</a><br/>
    <a href="${ pageContext.request.contextPath }/UserAction_update.action">修改用户</a><br/>
    <a href="${ pageContext.request.contextPath }/UserAction_delete.action">删除用户</a><br/>
    <a href="${ pageContext.request.contextPath }/UserAction_save.action">保存用户</a><br/>
    
    <h3>通过通配符的方式</h3>
    <a href="${ pageContext.request.contextPath }/ProductAction_find.action">查询商品</a><br/>
    <a href="${ pageContext.request.contextPath }/ProductAction_update.action">修改商品</a><br/>
    <a href="${ pageContext.request.contextPath }/ProductAction_delete.action">删除商品</a><br/>
    <a href="${ pageContext.request.contextPath }/ProductAction_save.action">保存商品</a><br/>
        
    <h3>通过动态方法访问的方式</h3>
    <a href="${ pageContext.request.contextPath }/customer!find.action">查询客户</a><br/>
    <a href="${ pageContext.request.contextPath }/customer!update.action">修改客户</a><br/>
    <a href="${ pageContext.request.contextPath }/customer!delete.action">删除客户</a><br/>
    <a href="${ pageContext.request.contextPath }/customer!save.action">保存客户</a><br/>
</body>
</html>

成功执行:


16102290-b798297c1516dc7f.png
image.png

END

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值