框架(frameWork):某一種應用的半成品
struts2: 表現層 處理與頁面進行交互的相關功能
hibernate: 持久層 負責業務邏輯數據的持久化
spring: 業務層 負責複雜的業務邏輯
mybatis: 持久層 負責業務邏輯數據的持久化 跟數據庫作交互 html
struts1 與 struts2 的區別:
1.struts2 是在webwork2的基礎上發展而來的,他不依賴於struts API 和 servlet API
2.struts2提供了攔截器,利用攔截器能夠進行AOP(切面)編程
3.struts2提供了類型轉換器,可使用轉換器把參數轉化成咱們須要的類型
4.struts2提供了多種表現層技術
5.struts2的輸入校驗能夠對指定的方法進行校驗
6.struts2提供了全局範圍,包範圍、action範圍 的國際化資源文件管理
※struts2太靈活,不易控制java
搭建 struts2 的開發環境:
1.導入jar包:找到所需的jar包:發行包的lib目錄中web
2.在工程 src 下編寫struts2的配置文件 struts2.xmlspring
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <constant name="struts.devMode" value="true"></constant> 7 8 <package name="ly" namespace="/" extends="struts-default"> 9 <action name="helloWorld" class="cn.gs.ly.HelloAction" method="sayHello"> 10 <result name="success" type="dispatcher">/1.jsp</result> 11 </action> 12 </package> 13 </struts>
3. 配置核心控制器,就是一個過濾器 web.xml數據庫
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>Struts2</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 <filter> 14 <filter-name>struts2</filter-name> 15 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 16 </filter> 17 <filter-mapping> 18 <filter-name>struts2</filter-name> 19 <url-pattern>/*</url-pattern> 20 </filter-mapping> 21 22 23 </web-app>
註解方式 apache
1.導入asm等相關jar包編程
2.web.xml 添加struts 2 核心過濾器tomcat
3.src目錄下新建com.yzpc.action包,並在該包下建立LoginAction類繼承ActionSupport類mybatis
一、編寫struts.xml配置文件app
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts><!--這是Struts2配置文件的根元素--> 6 <constant name="struts.devMode" value="true"></constant><!-- 指定容器自動加載 --> 7 8 <package name="ly" namespace="/test" extends="struts-default"> 9 <!-- 10 pageckage:方便管理動做元素 11 name:必須有包的名稱,配置文件中必須保證惟一。 12 namespace:該包的名稱空間,通常是以"/"開頭 13 extends:繼承的父包的名稱。struts-default名稱的包是struts2框架已經命名好的一個包。(在struts2-core.jar的struts-default.xml中) 14 abstract:是不是抽象包。沒有任何action元素的包就是抽象包(java類) 15 --> 16 <action name="helloWorld" class="cn.gs.ly.HelloAction" method="sayHello"> 17 <!-- 18 action:表明一個請求動做 19 name:動做的名稱,同包中必須惟一。 20 class:負責處理的JavaBean的類全名。動做所對應的處理類,包全名.類名 21 method:JavaBean類中的對應處理方法。(動做方法:特色是,public String 方法名(){}) 22 --> 23 <result name="success" type="dispatcher">/1.jsp</result> 24 <!-- 25 result:結果類型 26 type:跳轉的方式 27 name:動做方法返回的字符串 28 主體內容:跳轉的具體地址 29 --> 30 </action> 31 </package> 32 </struts>
2. 根據配置文件,建立須要的javabean和對應的動做方法, 在動做方法中完成邏輯調用。
1 package cn.gs.ly; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class HelloAction extends ActionSupport{ 6 private String message; 7 8 public String getMessage() { 9 return message; 10 } 11 12 public void setMessage(String message) { 13 this.message = message; 14 } 15 16 public String sayHello(){ 17 message = "hello nice to meet you!"; 18 return "success"; 19 } 20 21 22 @Override 23 public String execute() throws Exception { 24 // TODO Auto-generated method stub 25 return "success"; 26 } 27 28 }
※struts 2 中預約義的ResultType
三、編寫網頁文件,顯示結果
1 <%@ page language="java" contentType="text/html" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 ${message} 11 <h1>1.jsp</h1> 12 </body> 13 </html>
四、訪問helloworld動做的方式:
http://localhost:8080/struts2/test/helloworld 應用名稱/包的名稱空間/動做的名稱
1 http://localhost:8080/struts2day01/test/a/b/c/helloworld 2 /test/a/b/c:名稱空間 3 helloworld:動做名稱 4 5 搜索順序:名稱空間 6 /test/a/b/c 沒有helloworld 7 /test/a/b 沒有helloworld 8 /test/a 沒有helloworld 9 /test 有了,調用執行
struts.xml 中的各項默認值 package若是沒有namespace 默認namespace="/"; action若是沒有class,就會找 com.opensymphony.xwork2.ActionSupport action若是沒有method,就會找 public String execute(){} result name的常量值: SUCCESS:success; NONE:none; ERROR:error; INPUT:input; LOGIN:login; result若是沒有 type,默認是dispatcher <result-types> <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/> <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/> <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/> <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/> <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/> <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/> <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/> <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/> <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/> <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" /> <result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" /> </result-types> dispatcher:普通轉發到某個頁面 chain:普通抓發到某個動做 redirect:重定向到某一頁面 redirectAction:重定向到某一動做 plainText:以純文本的形式輸出jsp的內容 動態設置action屬性 <action name="helloWorld6" class="cn.gs.wwg.HelloAction" > <param name="message">i am best cool</param> <result name="success" >/5.jsp</result> </action> 取得action屬性 <result name="success" type="dispatcher">/6.jsp?msg=${message}</result> ---6.jsp中取值 : ${param.msg} 控制action的後綴名:指定須要struts2處理的請求後綴 <constant name="struts.action.extension" value="do"></constant> 開發中配置文件的更改,在訪問時讓框架自動從新加載:指定容器自動加載 <constant name="struts.devMode" value="true"></constant>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <constant name="struts.devMode" value="true"></constant> 7 <constant name="struts.action.extension" value="do,,action"></constant> 8 9 <!-- result 元素寫法,方式一。 --> 10 <package name="ly" namespace="/" extends="struts-default"> 11 <action name="helloWorld" class="cn.gs.ly.HelloAction" method="sayHello"> 12 <result name="success" type="dispatcher">/1.jsp</result> 13 </action> 14 </package> 15 16 <!-- result 元素寫法,方式二。 --> 17 <package name="ly2" namespace="/test2" extends="struts-default"> 18 <action name="helloWorld2" class="cn.gs.ly.HelloAction" method="sayHello"> 19 <result name="success" type="chain">helloWorld21</result> 20 </action> 21 <action name="helloWorld21"> 22 <result name="success" type="dispatcher">/2.jsp</result> 23 </action> 24 </package> 25 26 <package name="ly3" namespace="/test3" extends="struts-default"> 27 <action name="helloWorld3" class="cn.gs.ly.HelloAction" method="sayHello"> 28 <result name="success" type="redirectAction"> 29 <param name="actionName">helloWorld31</param> 30 <param name="namespace">/test31</param> 31 </result> 32 </action> 33 </package> 34 <package name="ly31" namespace="/test31" extends="struts-default"> 35 <action name="helloWorld31"> 36 <result name="success" type="dispatcher">/3.jsp</result> 37 </action> 38 </package> 39 40 <package name="ly4" namespace="/test4" extends="struts-default"> 41 <action name="helloWorld4" > 42 <result name="success" type="plainText">/4.jsp</result> 43 </action> 44 </package> 45 46 <!-- 動態的給Action類的屬性賦值 --> 47 <package name="ly5" namespace="/test5" extends="struts-default"> 48 <action name="helloWorld5" class="cn.gs.ly.HelloAction"> 49 <param name="message">what is your name</param> 50 <result name="success" >/5.jsp</result> 51 </action> 52 </package> 53 54 <!-- 獲取action的值 --> 55 56 <package name="ly6" namespace="/test6" extends="struts-default"> 57 <action name="helloWorld6" class="cn.gs.ly.HelloAction" method="sayHello"> 58 <result name="success" type="dispatcher">/6.jsp?msg=${message}</result> 59 </action> 60 </package> 61 62 63 </struts> 64