1 result標簽是乾什麼的 就是結果,伺服器處理完返回給瀏覽器的結果;是一個輸出結果數據的組件 2 什麼時候需要指定result標簽的類型 把要輸出的結果數據按照我們指定的數據類型進行處理 3 常見的類型(在struts的預設配置文件120行有這些類型的列表) 3.1 dispatcher 轉發 ...
1 result標簽是乾什麼的
就是結果,伺服器處理完返回給瀏覽器的結果;是一個輸出結果數據的組件
2 什麼時候需要指定result標簽的類型
把要輸出的結果數據按照我們指定的數據類型進行處理
3 常見的類型(在struts的預設配置文件120行有這些類型的列表)
3.1 dispatcher 轉發(預設類型)
3.2 redirect 重定向URL
格式一
<result type="redirect">
網址(例如:http://www.baidu.com)
</result>
格式二(註意:param標簽的name屬性是固定值location)
<result type="redirect">
<param name="location">
http://www.baidu.com
</param>
</result>
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>cn.xiangxu</groupId> 4 <artifactId>ssh02</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <packaging>war</packaging> 7 <dependencies> 8 <dependency> 9 <groupId>org.apache.struts</groupId> 10 <artifactId>struts2-core</artifactId> 11 <version>2.3.8</version> 12 </dependency> 13 <dependency> 14 <groupId>org.apache.struts</groupId> 15 <artifactId>struts2-spring-plugin</artifactId> 16 <version>2.3.8</version> 17 </dependency> 18 </dependencies> 19 </project>maven依賴文件
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_2_5.xsd" version="2.5"> 3 <display-name>ssh02s</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 <!-- 配置spring監聽listener 14 用於初始化Spring容器 --> 15 <listener> 16 <listener-class> 17 org.springframework.web.context.ContextLoaderListener 18 </listener-class> 19 </listener> 20 21 <!-- 配置Spring配置文件的位置 --> 22 <context-param> 23 <param-name>contextConfigLocation</param-name> 24 <param-value>classpath:spring-*.xml</param-value> 25 </context-param> 26 27 <!-- 配置主控制器和過濾條件 --> 28 <filter> 29 <filter-name>mvc</filter-name> 30 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 31 </filter> 32 <filter-mapping> 33 <filter-name>mvc</filter-name> 34 <url-pattern>/*</url-pattern> 35 </filter-mapping> 36 37 </web-app>web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" 5 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" 7 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 8 xsi:schemaLocation=" 9 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 11 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd 12 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 13 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 14 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 15 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 16 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 17 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> 18 19 <!-- 配置組件掃描 --> 20 <context:component-scan base-package="cn.xiangxu" /> 21 22 </beans>spring_context.xml
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 7 <package name="type" namespace="/type" extends="struts-default"> 8 <action name="redirect" class="redirectAction"> <!-- 後臺經過處理後,再進行重定向 --> 9 <result name="redirect" type="redirect"> 10 http://www.baidu.com 11 </result> 12 </action> 13 14 <action name="cq"> <!-- 直接重定向,在後臺不進行任何處理 --> 15 <result type="redirect"> 16 http://cq.qq.com/ 17 </result> 18 </action> 19 20 <action name="dzsk"> <!-- 利用格式二實現 --> 21 <result type="redirect"> 22 http://www.dzshike.com/ 23 </result> 24 </action> 25 </package> 26 27 </struts>struts.xml
1 package cn.xiangxu.action; 2 3 import org.springframework.context.annotation.Scope; 4 import org.springframework.stereotype.Controller; 5 6 @Controller 7 @Scope("prototype") 8 public class RedirectAction { 9 public String execute() { 10 System.out.println("測試重定向URL"); 11 return "redirect"; 12 } 13 }RedirectAction
項目結構
3.3 redirectAction 重定向Action
格式一:請求路徑不變,僅僅改變請求名
<result name="xxx" type="redirectAction">
請求名/請求名.action
</result>
格式二:請求路徑和請求名都該改變
<result name="xxx" type="redirectAction">
<param name="namespace">/路徑名</param>
<param name="actionName">請求名/請求名.action</param>
</result>
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 7 <package name="test" namespace="/test" extends="struts-default"> <!-- namespace是配置訪問路徑,extends是配置繼承預設struts文件 --> 8 <action name="demo" class="testAction"> <!-- name是配置訪問網名,class是配置action類 --> 9 <result name="success"> 10 /WEB-INF/jsp/msg.jsp 11 </result> 12 </action> 13 </package> 14 15 <package name="type" namespace="/type" extends="struts-default"> 16 17 18 <action name="demo" class="testAction"> 19 <result name="success"> 20 /WEB-INF/jsp/msg.jsp 21 </result> 22 </action> 23 24 <action name="redirectAction01" class="redirectActionAction"> <!-- 重定向1:只改變請求網名 --> 25 <result name="redirectAction" type="redirectAction"> 26 demo 27 </result> 28 <result name="redirectAction" type="redirectAction"> 29 demo 30 </result> 31 </action> 32 <action name="redirectAction02" class="redirectActionAction"> <!-- 重定向2:路徑和網名都改變 --> 33 <result name="redirectAction" type="redirectAction"> 34 <param name="namespace">/test</param> 35 <param name="actionName">demo</param> 36 </result> 37 </action> 38 39 </package> 40 41 </struts>struts.xml
1 package cn.xiangxu.action; 2 3 import org.springframework.context.annotation.Scope; 4 import org.springframework.stereotype.Controller; 5 6 @Controller 7 @Scope("prototype") 8 public class RedirectActionAction { 9 public String execute() { 10 System.out.println("測試重定向action"); 11 return "redirectAction"; 12 } 13 }RedirectActionAction.java
項目結構
3.4 stream 流,處理圖片
3.4.1 格式
<result name="success" type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">imageStream</param>
<param name="contentDisposition">attachment;filename="document.pdf"</param>
<param name="bufferSize">1024</param>
</result>
3.4.2 參數解釋
contentType 定義媒體類型
inputName 必須是一個inputStream類型的流
contentDisposition 強制下載保存(可選)
bufferSize 指定緩存區域的大小(可選)
3.4.3 案例:向瀏覽器發送圖片數據並顯示
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 7 8 <package name="type" namespace="/type" extends="struts-default"> 9 10 11 <action name="stream" class="streamAction"> 12 <result name="success" type="stream"> 13 <param name="contentType">image/png</param> 14 <param name="inputName">image</param> <!-- image是ImageAction中的一個成員變數 --> 15 </result> 16 </action> 17 18 </package> 19 20 21 22 </struts>struts.xml
1 package cn.xiangxu.action; 2 3 import java.awt.Color; 4 import java.awt.Graphics2D; 5 import java.awt.image.BufferedImage; 6 import java.io.ByteArrayInputStream; 7 import java.io.ByteArrayOutputStream; 8 import java.io.IOException; 9 import java.io.InputStream; 10 11 import javax.imageio.ImageIO; 12 13 import org.springframework.context.annotation.Scope; 14 import org.springframework.stereotype.Controller; 15 16 @Controller 17 @Scope("prototype") 18 public class StreamAction { 19 20 private InputStream image; 21 22 public String execute() throws IOException { 23 // 設置圖片 24 BufferedImage img = new BufferedImage(399, 60, BufferedImage.TYPE_3BYTE_BGR); 25 26 // 獲取畫筆 27 Graphics2D g = img.createGraphics(); 28 g.setColor(Color.white); 29 g.drawString("hello fury,測試stream", 10, 30); 30 31 // 將圖片轉成位元組數組 32 ByteArrayOutputStream out = new ByteArrayOutputStream(); 33 ImageIO.write(img, "png", out); 34 out.close(); 35 36 // 將位元組數組放到位元組流中 37 byte [] data = out.toByteArray(); 38 image = new ByteArrayInputStream(data); 39 40 return "success"; 41 } 42 43 public InputStream getImage() { 44 return image; 45 } 46 47 public void setImage(InputStream image) { 48 this.image = image; 49 } 50 }StreamAction.java
項目結構
3.5 json 用於處理ajax請求
導包:struts2-json-plugin
註意:package標簽中的extends屬性值變更為json-default(json-default繼承了struts-default)
用法:兩種方式
返回Action中的所有值
<result name="xxx" type="json"></result>
返回Action中的單個屬性值
<result name="xxx" type="json">
<param name="root">action中的屬性名</param> <!-- name的屬性值是固定的 -->
</result>
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 7 8 <package name="type" namespace="/type" extends="json-default"> 9 10 11 <action name="json" class="jsonAction"> <!-- 返回多個值 --> 12 <result name="success" type="json"></result> 13 </action> 14 15 <action name="json02" class="jsonAction"> <!-- 返回一個值 --> 16 <result name="success" type="json"> 17 <param name="root">name</param> 18 </result> 19 </action> 20 21 22 </package> 23 24 25 </struts>struts.xml
1 package cn.xiangxu.action; 2 3 import org.springframework.context.annotation.Scope; 4 import org.springframework.stereotype.Controller; 5 6 @Controller 7 @Scope("prototype") 8 public class JsonAction { 9 private String name; 10 private Integer age; 11 12 public String execute() { 13 name = "wys"; 14 age = 100; 15 return "success"; 16 } 17 18 public String getName() { 19 return name; 20 } 21 public void setName(String name) { 22 this.name = name; 23 } 24 public Integer getAge() { 25 return age; 26 } 27 public void setAge(Integer age) { 28 this.age = age; 29 } 30 31 32 }JsonAction.java
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>cn.xiangxu