struts2 2.5版本(一)

struts2 2.5版本(一)

struts2 2.5版本(二)

struts2 2.5版本(三)

入门

下载地址:https://struts.apache.org/

42b557823f23b12632ee5fcd1c72d9fefda.jpg

解压后的目录

0ac01fec183781d71a2ce0338c4503b127e.jpg

创建web工程,配置Tomcat,导入包

e65216c35d65627405b2ab361760f96c94c.jpg

HelloAction

package com.company.hello;

public class HelloAction {

    public String hello() {

        System.out.println("hello world!");

        return "success";
    }
}

src下创建 struts.xml

<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <package name="hello" namespace="/hello" extends="struts-default">
        <action name="HelloAction" class="com.company.hello.HelloAction" method="hello">
            <result name="success">/hello.jsp</result>
        </action>

    </package>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!-- 配置struts2核心过滤器-->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
   <filter-mapping>
       <filter-name>struts2</filter-name>
       <url-pattern>/*</url-pattern>
   </filter-mapping>

</web-app>

创建 hello.jsp

<%--
  Created by IntelliJ IDEA.
  User: mac
  Date: 2019/3/9
  Time: 5:27 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>hello world</h1>
</body>
</html>

浏览器输入 http://localhost:8080/hello/HelloAction

a6bfd6de50c139c568ad897ffd8af28d129.jpg

struts2.xml配置

84d5329736350ca63903fa7cb4b24582e7d.jpg

<struts>
    <!-- package:将Action配置封装.可以在Package中配置很多action.
			name: 包名,标识作用,不能其他包名重复
			namespace: 给action的访问路径中定义一个命名空间
			extends: 继承一个 指定包
			abstract: 包是否为抽象的; 标识性属性.标识该包不能独立运行.专门被继承
	  -->
	<package name="hello" namespace="/hello" extends="struts-default" >
		<!-- action:配置action类
				name: 决定了Action访问资源名.
				class: action的完整类名
				method: 指定调用Action中的哪个方法来处理请求
		 -->
        <action name="HelloAction" class="com.company.hello.HelloAction" method="hello">
			<!-- result:结果配置 
					name: 标识结果处理的名称.与action方法的返回值对应.
					type: 指定调用哪一个result类来处理结果,默认使用转发.
					标签体:填写页面的相对路径
			-->
			<result name="success" type="dispatcher" >/hello.jsp</result>
		</action>
	</package>
</struts>

struts2常量配置

修改struts2常量配置

方式先后,也是配置文件的加载顺序,后者会覆盖先加载的

方式一:struts.xml 

<struts>   
   <constant name="struts.i18n.encoding" value="UTF-8"></constant>
</struts>

方式二:strtus.properties

struts.i18n.encoding=UTF8

方式三:web.xml

<context-param>
   <param-name>struts.i18n.encoding</param-name>
   <param-value>UTF-8</param-value>
</context-param>

常量配置

<struts>
    <!-- i18n:国际化. 解决post提交乱码 -->
    <constant name="struts.i18n.encoding" value="UTF-8"></constant>
    
    <!-- 指定反问action时的后缀名
        http://localhost:8080/hello/HelloAction.do
    -->
    <constant name="struts.action.extension" value="do"></constant>
    
    <!-- 指定struts2是否以开发模式运行
            1.热加载主配置.(不需要重启即可生效)
            2.提供更多错误信息输出,方便开发时的调试
            (开发设置true,上架设置false)
     -->
    <constant name="struts.devMode" value="true"></constant>
    
    <!-- 引入其他struts配置文件 -->
    <include file="com/company/hello/struts.xml"></include>
</struts>

动态方法调用

方式一:

配置 struts.enable.DynamicMethodInvocation

<!-- 配置动态方法常量,默认是关闭的,需要开启 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

配置 global-allowed-methods

<global-allowed-methods>regex:.*</global-allowed-methods>

struts.xml 如下

<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <!-- 配置动态方法常量,默认是关闭的,需要开启 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

    <package name="dynamic" namespace="/dynamic" extends="struts-default">
        <global-allowed-methods>regex:.*</global-allowed-methods>
        <!--或把方法用逗号分开-->
        <!--<global-allowed-methods>add,delete</global-allowed-methods>-->

        <action name="Demo" class="com.company.hello.Demo">
            <result name="add">/add.jsp</result>
            <result name="delete">/delete.jsp</result>
        </action>
    </package>
</struts>

Demo 类

package com.company.hello;

public class Demo {

    public String add() {
        System.out.println("add");
        return "add";
    }

    public String delete() {
        System.out.println("delete");
        return "delete";
    }
}

访问方式为 http://localhost:8080/dynamic/Demo!delete.do

5ed272f550a441d174fe9b1431ac99baf7e.jpg

访问方式为 http://localhost:8080/dynamic/Demo!add.do

9682bd82b08cdd94cd450c97c6129852ea7.jpg

方式二:通配符

<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <package name="dynamic" namespace="/dynamic" extends="struts-default">
        <global-allowed-methods>regex:.*</global-allowed-methods>

        <action name="Demo_*" class="com.company.hello.Demo" method="{1}">
            <result name="add">/add.jsp</result>
            <result name="delete">/delete.jsp</result>
        </action>
    </package>
</struts>

访问方式为:

http://localhost:8080/dynamic/Demo_add.do

http://localhost:8080/dynamic/Demo_delete.do

默认配置

<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <package name="default" namespace="/default" extends="struts-default">

        <!--如果找不到包下的action,会使用Demo作为默认action处理请求-->
        <default-action-ref name="Demo"></default-action-ref>
        
        <!-- method属性默认值 execute -->
        <!-- class默认值 com.opensymphony.xwork2.ActionSupport -->
        <action name="Demo" class="com.company.demo.Demo" method="add">
            <!-- name默认属性 success -->
            <!-- type默认值 dispatcher -->
            <result name="add">/add.jsp</result>
        </action>
    </package>
</struts>

action类

方式1:不用继承任何父类,也不需要实现任何接口

package com.company.action;

public class Demo {
    
}

方式2:实现一个接口Action

package com.company.action;

import com.opensymphony.xwork2.Action;

public class Demo2 implements Action {

    @Override
    public String execute() throws Exception {
        return null;
    }
}

方式3:继承一个类 ActionSupport

package com.company.action;

import com.opensymphony.xwork2.ActionSupport;

public class Demo3 extends ActionSupport {

}

 

转载于:https://my.oschina.net/gwlCode/blog/3020278

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="helloworld" class="com.mytest.HelloWorldAction"> <result> /result.jsp </result> </action> </package> <package name="LoginForm" extends="struts-default"> <action name="login" class="com.mytest.LoginAction" method="execute"> <result> /login.jsp </result> </action> </package> </struts> <!--1.使用 struts2.5.16 版本 2.lib 文件夹下放置:工程所需jar包 3.xml标签库为远程获取,路径:http://struts.apache.org/dtds/struts-2.5.dtd 可设置为本地【xml输入语法快捷提示】,就不用远程获取了:window-->preference-->输入Catalog-->xml下的Catalog-->Add-->location:解压缩struts-core-2.5.16.jar 后,文件struts-2.5.dtd文件路径。 4.设置开发者模式: <constant name="struts.devMode" value="true" /> <constant name="struts.i18n.encoding" value="utf-8" /> 每次HTTP请求系统都重新加载资源文件,有助于开发 5.struts配置文件改动后,是否重新加载 <constant name="struts.configuration.xml.reload" value="true" /> 6.查看源码:Build path 后的类库中,奶瓶图标找到struts-core-2.5.16.jar 右键-->properties-->java Source Attachment-->External location :源码路径 7.查看文档API:Build path 后的类库中,奶瓶图标找到struts-core-2.5.16.jar 右键-->properties-->javadoc location :输入网址 或选择源码DOC目录 8.拦截器:web.xml 配置拦截器<filter> struts2.5的filter-class 与struts2.5以前版本有所不同 <!-- 浏览器访问 http://localhost:8080/MyWeb/helloworld --> --> <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts 2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置核心拦截器 --> <filter> <filter-name>struts2</filter-name> <!-- Filter的实现类 struts2.5 --> <filter-class> org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <!-- 拦截所有的url --> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值