一:Struts是什麼html
Struts:是用來處理訪問服務器的請求。java
二:搭建Struts框架web
1.導包apache
2.書寫Action類服務器
public class TestAction extends ActionSupport { public String test() { System.out.println("通常用這種方法建立Action類"); return "success"; } }
3.書寫核心配置文件app
<?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"> <struts> <!-- package:將Action配置封裝.就是能夠在Package中配置不少action. name屬性: 給包起個名字,起到標識做用.隨便起.不能其餘包名重複. namespace屬性:給action的訪問路徑中定義一個命名空間 extends屬性: 繼承一個 指定包 abstract屬性:包是否爲抽象的; 標識性屬性.標識該包不能獨立運行.專門被繼承 --> <package name="test" namespace="/test" extends="struts-default"> <!-- action元素:配置action類 name屬性: 決定了Action訪問資源名. class屬性: action的完整類名 method屬性: 指定調用Action中的哪一個方法來處理請求 --> <action name="TestAction" class="cn.itcast.manager.TestAction" method ="test"> <!-- result元素:結果配置 name屬性: 標識結果處理的名稱.與action方法的返回值對應. type屬性: 指定調用哪個result類來處理結果,默認使用轉發. 標籤體:填寫頁面的相對路徑 --> <result name="success" type="dispatcher">/test.jsp</result> </action> </package> </struts>
4.將struts2核心過濾器配置到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"> <display-name>StructsDay01</display-name> <!-- struts2核心過濾器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>