zoukankan      html  css  js  c++  java
  • Struts2的配置

    第一次搭建Sturts框架会遇到几个坑

    1.IntelliJ自动生成的web.xml中filter-class报错,是因为Struts2-2.5版本修改了

    改为

    2.配置好Tomcat后会报错

    可是我明明已经配正确了啊?修复的地方如下(这是最坑的地方)

    回到我的Android Studio一看,果然真有一样的

    3.接下来又会报个错,尼玛,错误我就不发了,反正是少了个jar包

    添加图中的xwork-core-2.1.6.jar然后居然还得Fix一下,终于好了

    我尼玛一行代码还没写啊,都折腾个半死,IntelliJ该好好反省一下

    好了,下面开始讲Struts2的配置

    我的Demo工程结构如下

    先把公用的代码贴出来

    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>
        <include file="com/jinke/struts2/demo1/struts_demo1.xml"/>
        <include file="com/jinke/struts2/demo2/struts_demo2.xml"/>
        <include file="com/jinke/struts2/demo3/struts_demo3.xml"/>
        <include file="com/jinke/struts2/demo4/struts_demo4.xml"/>
        <include file="com/jinke/struts2/demo5/struts_demo5.xml"/>
    </struts>

    web.xml(welcome-file是tomcat启动后自动弹出的首页,一个工程也只有一个)

    <?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">
        <welcome-file-list>
            <welcome-file>demo.jsp</welcome-file>
        </welcome-file-list>
        <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>

    第一部分:最基础的

    public class HelloAction {
        public String execute() {
            System.out.println("helloaaction执行了");
            return "success";
        }
    }

    struts_demo1.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" extends="struts-default" namespace="/">
            <action name="hello" class="com.jinke.struts2.demo1.HelloAction">
                <!--配置页面的跳转-->
                <result name="success">success.jsp</result>
            </action>
        </package>
    </struts>

    index.jsp

    <%--
      Created by IntelliJ IDEA.
      User: wanglei
      Date: 2019/6/15
      Time: 11:13
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Struts入门</title>
    </head>
    <body>
    <h1>Struts2入门</h1>
    <h3><a href="${ pageContext.request.contextPath }/hello.action">Struts2的入门</a></h3>
    </body>
    </html>

    success.jsp

    <%--
      Created by IntelliJ IDEA.
      User: wanglei
      Date: 2019/6/15
      Time: 12:05
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>成功</title>
    </head>
    <body>
    <h1>跳转页面成功!</h1>
    </body>
    </html>

    结果

    第二部分:实现Action的三种方式

    public class ActionDemo2 {
        public String execute() {
            System.out.println("ActionDemo2执行了");
            return null;
        }
    }
    import com.opensymphony.xwork2.Action;
    
    /**
     * action实现方法2:实现接口
     */
    public class ActionDemo3 implements Action {
        @Override
        public String execute() throws Exception {
            System.out.println("ActionDemo3执行了");
            return null;
        }
    }
    import com.opensymphony.xwork2.ActionSupport;
    
    /**
     * action实现方式3:继承actionSupport
     */
    public class ActionDemo4 extends ActionSupport {
        @Override
        public String execute() throws Exception {
            System.out.println("actiondemo4执行了");
            return NONE;
        }
    }

    struts_demo2.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="demo2" extends="struts-default" namespace="/">
            <action name="actionDemo2" class="com.jinke.struts2.demo2.ActionDemo2"/>
        </package>
    </struts>

    struts_demo3.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="demo3" extends="struts-default" namespace="/">
            <action name="actionDemo3" class="com.jinke.struts2.demo3.ActionDemo3"/>
        </package>
    </struts>

    struts_demo4.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="demo4" extends="struts-default" namespace="/">
            <action name="actionDemo4" class="com.jinke.struts2.demo4.ActionDemo4"/>
        </package>
    </struts>

    第三部分:实现方法的三种方式

    import com.opensymphony.xwork2.ActionSupport;
    
    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;
        }
    }
    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;
        }
    }
    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;
        }
    
    
    }

    struts_demo5.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"/>
    
        <package name="demo5" extends="struts-default" namespace="/">
            <action name="userFind" class="com.jinke.struts2.demo5.UserAction" method="find"></action>
            <action name="userUpdate" class="com.jinke.struts2.demo5.UserAction" method="update"></action>
            <action name="userDelete" class="com.jinke.struts2.demo5.UserAction" method="delete"></action>
            <action name="userSave" class="com.jinke.struts2.demo5.UserAction" method="save"></action>
    
            <!--通配符方式-->
            <action name="product_*" class="com.jinke.struts2.demo5.ProductAction" method="{1}">
                <allowed-methods>find,update,delete,save</allowed-methods>
            </action>
    
            <!--动态方法访问的方式-->
            <action name="customer" class="com.jinke.struts2.demo5.CustomerAction">
                <allowed-methods>find,update,delete,save</allowed-methods>
            </action>
    
        </package>
    </struts>

    这里有个坑:struts2在2.5之后需要在Struts.xml中增加可用的方法

    <allowed-methods>find,update,delete,save</allowed-methods>

    结果

    Struts2执行流程
    先过滤器(前端控制器)--> 执行拦截器 --> 找到struts2.xml配置文件 --> 找到Action的类 --> 反射执行里面的方法 --> 判断返回值,进行页面跳转

    Struts2配置文件加载顺序
    init_DefaultProperties 加载default.properties
    init_TraditionalXmlConfigurations 加载struts-defalut.xml、struts-plugin.xml、struts.xml
    init_LegacyStrutsProperties 加载struts.properties
    init_CustomConfigurationProviders 加载配置提供类
    init_FilterInitParameters 加载web.xml中过滤器初始化参数
    init_AliasStandardObjects 加载Bean对象

    欢迎关注的微信公众号:安卓圈

  • 相关阅读:
    lucene .NET 搜索图片 功能实现
    (转)权威支持: 选择正确的 WebSphere 诊断工具
    (转)WebSphere 中池资源调优
    (转)使用 DB2 HADR 选择用于灾难恢复的 SUPERASYNC 模式
    (转) DB2 HADR
    (转)DB2 HADR 监控详解
    (转)python高级FTP
    (转)Python的web服务器
    (转)python通过paramiko实现,ssh功能
    (转)request模拟知乎登录(无验证码机制
  • 原文地址:https://www.cnblogs.com/anni-qianqian/p/11026972.html
Copyright © 2011-2022 走看看