第一次搭建Sturts框架會遇到幾個坑html
1.IntelliJ自動生成的web.xml中filter-class報錯,是由於Struts2-2.5版本修改了前端
改成java
2.配置好Tomcat後會報錯web
但是我明明已經配正確了啊?修復的地方以下(這是最坑的地方)apache
回到個人Android Studio一看,果真真有同樣的tomcat
3.接下來又會報個錯,尼瑪,錯誤我就不發了,反正是少了個jar包微信
添加圖中的xwork-core-2.1.6.jar而後竟然還得Fix一下,終於好了app
我尼瑪一行代碼還沒寫啊,都折騰個半死,IntelliJ該好好檢討一下框架
好了,下面開始講Struts2的配置jsp
個人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對象
歡迎關注的微信公衆號:安卓圈