項目結構:html
一、用IDEA新建一個SpringBoot+Maven的項目java
二、新建的項目是沒有webapp、WEB-INF、與web.xml文件的,須要在下圖中添加:web
三、在pom.xml引入struct2apache
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5.18</version> </dependency>
四、編寫主頁home.jsp:瀏覽器
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Home</title> </head> <body> <form action="hello"> <label for="pername">Please enter your name</label> <input id="pername" type="text" name="name" /> <input type="submit" value="提交" /> </form> </body> </html>
該頁面輸入一個名字,經過名爲hello的action,將名字顯示在另外一個頁面。服務器
五、定義動做HelloWorldAction.javaapp
public class HelloWorldAction { private String name; public String excute(){ return "success"; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
六、jsp頁面與動做之間須要經過WEB-INF\classes\struts.xml關聯,且須要在web.xml中定義過濾器webapp
struts.xml以下:jsp
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 設置struts是否爲開發模式,默認爲false,測試階段通常設爲true. --> <constant name="struts.devMode" value="true" /> <package name="suibian" extends="struts-default"> <action name="hello" class="com.owlforest.home.action.HelloWorldAction" method="excute"> <result name="success">/HelloWorld.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"> <display-name>Struts 2</display-name> <welcome-file-list> <welcome-file>home.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>
七、編寫跳轉頁HelloWorld.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>show name</title> </head> <body> Hello World : <s:property value="name" /> </body> </html>
須要注意的是,home.jsp、HelloWorldAction.java與HelloWorld.jsp中的name屬性須要在名稱上面保持一致。
form表單中的action名字與struct.xml中的action name要保持一致
這個地方在屬性過多的狀況下可能會出現命名衝突致使錯誤出現,不知道struct2是如何解決的,後續再繼續學習。
八、result的type參數
上面的struts.xml配置的result爲:
<result name="success">/HelloWorld.jsp</result>
這實際上時一種簡寫的形式,完整的形式爲:
<result name="success" type="dispatcher"> <param name="location">/HelloWorld.jsp</param> </result>
dispatcher結果類型是默認的類型。它用於轉發到服務器上的servlet,JSP,HTML等頁面。它使用RequestDispatcher.forward()方法。
除了上述的dispatcher類型外,還有redirect、freemarker類型。
redirect結果類型調用標準的response.sendRedirect()方法,使得瀏覽器向給定的位置建立一個新請求。
<result name="success" type="redirect"> <param name="location">https://www.baidu.com</param> </result>
freemarker是一個流行的模板引擎,使用預約義的模板生成輸出。這與JSP相似。使用方法以下:
在webapp下新建文件 hello.fm
,內容爲:
What's your name ?${name}
修改struts.xml便可完成配置。
<result name="success" type="freemarker"> <param name="location">/hello.fm</param> </result>