在package
標籤中配置global-results
標籤java
<package name="xxx" namespace="/" extends="struts-default"> <global-results> <result name="xxx">xxx.jsp</result> </global-results> </package>
做用是爲package中配置的全部action提供全局的返回結果頁面web
在action
標籤中配置result
標籤json
<action name="xxx" method="xxx" class="xxx"> <result>xxx.jsp</result> </action>
做用是爲當前action配置結果頁面緩存
結果類型是在父類配置struts-default中定義的,struts框架默認實現了絕大多數的。
經常使用的結果類型有:服務器
dispatcher:轉發。爲result標籤的默認值。app
<result type="dispatcher">xxx.jsp</result>
由action轉發到jsp。框架
result標籤中配置的結果必須是jsp頁面jsp
chain:轉發。ide
<result type="chain"> <param name="actionName">xxx.action</param> </result>
由action轉發到action。函數
result標籤中配置的結果必須是action
redirect:重定向。
<result type="redirect"> <param name="location">xxx.jsp</param> </result>
由action重定向到jsp。
result標籤中配置的結果必須是jsp頁面
redirectAction:重定向。
<result type="redirectAction"> <param name="actionName">xxx.action</param> </result>
由action重定向到action。
result標籤中配置的結果必須是action
stream: 結果爲文件流。文件下載
<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>
contentType:下載文件類型
contentDisposition:下載到客戶端時,客戶端文件名稱
bufferSize:讀文件的緩存大小
inputName:對應要輸出到客戶端流聲明的名稱
json:轉發。
<package name="xxx" namespace="/xxx" extends="json-default"> <action name="json" method="xxx" class="org.itpx.struts.action.JsonAction"> <result name="success" type="json"> <param name="encoding">utf-8</param> <param name="root">jsonObject</param> </result> </action> </package>
由action轉發到json結果
encoding:配置編碼格式
root:配置對象。action類中必須提供一個和root值相同的屬性名稱,且須要提供getter方法。
jsonActionRedirect: 重定向。
<action name="xxx" method="xxx" class="org.ithpx.struts.action.JsonAction"> <result name="success" type="jsonActionRedirect"> xxx.action </result> </action>
由action重定向到json結果。
struts2-json-plugin-xxx.jar
json-default
客戶端與服務端交互時,一般會帶參數過來,服務器也會回寫數據給客戶端。在此過程當中,參與着請求,和響應,以及會話。servlet在此過程當中提供了HttpServletRequest做爲獲取請求數據的方案,HttpServletResponse做爲響應的方案,HttpSession負責了會話方案。Struts實際上是基於servlet實現的web框架,他須要遵循規則提供請求,響應和會話的API供開發人員使用,所以Struts針對這一問題提供了本身的一套API封裝,提供了多種方式的訪問。
ActionContext對象實例獲取
ActionContext context = ActionContext.getContext();
ActionContext是綁定在當前線程中的。框架在請求線程開啓時,將ActionContext實例對象就綁定上來了,所以咱們能夠經過靜態方法直接獲取
請求參數的得到
Map<String, Object> parameters = context.getParamters();
至關於Servlet中的
request.getParamters()
方法
數據回顯到頁面
context.put(key, value);
至關於Servlet中的
request.setAttribute()
方法
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
<input type="text" name="name"> <input type="text" name="password">
action標籤
中配置param標籤
<param name="name" /> <param name="password" />
private String name; private String password; public void setName(String name) { this.name = name; } public void setPassword(String password) { this.password = password; }
- 要求頁面表單中的key和param標籤中name一致
- 要求頁面表單中的key和aciotn類中的屬性名一致
- action類中的屬性須要提供setter方法
<input type="text" name="name"> <input type="text" name="password">
private String name; private String password; public void setName(String name) { this.name = name; } public void setPassword(String password) { this.password = password; }
- 要求頁面中提供的key和action類中屬性名稱必須一致
- action類中必須提供屬性的setter方法
<input type="text" name="user.name"> <input type="text" name="user.password">
private User user; public void setUser(User user) { this.user = user; } public User getUser() { return user; }
- 頁面中的key至關於
對象.屬性
- action中的對象名稱須要要和頁面中key的對象名稱相同
- action中的對象中的屬性名稱要和頁面中key的對象的屬性相同
- action中的對象必須提供無參構造函數
- action中的對象必須提供getter和setter
<input type="text" name="name"> <input type="text" name="password">
public class Action03 extends ActionSupport implements ModelDriven<User> { private User user; @Override public User getModel() { if (user == null) { user = new User(); } return user; } }
頁面中的key的名稱須要和模型對象的名稱一致
<input type="text" name="list[0].name"> <input type="text" name="list[0].age"> <input type="text" name="list[1].name"> <input type="text" name="list[1].age">
public class DataAction05 extends ActionSupport { private List<User> list; public List<User> getList() { return list; } public void setList(List<User> list) { this.list = list; } public String test() { System.out.println(list); return SUCCESS; } }
<input type="text" name="map['a'].name"> <input type="text" name="map['a'].age"> <input type="text" name="map['b'].name"> <input type="text" name="map['b'].age">
public class DataAction05 extends ActionSupport { private Map<String, User> map; public Map<String, User> getMap() { return map; } public void setMap(Map<String, User> map) { this.map = map; } }
客戶端傳輸過程當中傳輸特定類型的字符串時,到action類中須要轉換爲對應的對象時,中間的轉換的問題。
主要解決的是對象類型的轉換
public class DateConversion extends StrutsTypeConverter { private SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); @Override public Object convertFromString(Map context, String[] values, Class toClass) { if (values != null && values.length > 0) { String dateString = values[0]; try { return sdf.parse(dateString); } catch (ParseException e) { e.printStackTrace(); return null; } } return null; } @Override public String convertToString(Map context, Object o) { if (o != null && o instanceof Date) { return sdf.format(o); } return null; } }
xwork-conversion.properties
,在其中配置要轉換的類型 java.util.Date=org.itheima.struts.action.DateConversion
UnknowHandler未知處理器是用來解決,用請求未知Action,或指定action裏的未知方法,或者action處理結束以後返回一個未知result時,沒有結果等狀況的
接口提供了三個方法要求子類實現。
handleUnknownAction內部實現
String newNamespace = "/unknow"; String newActionName = "test"; ConfigurationManager cfgManager = Dispatcher.getInstance().getConfigurationManager(); RuntimeConfiguration runtimeCfg = cfgManager.getConfiguration().getRuntimeConfiguration(); ActionConfig actionConfig = runtimeCfg.getActionConfig(newNamespace, newActionName); return actionConfig;
若是沒有找到請求的action,經過此方法轉到配置的新的action中。
所以,須要在配置中配置一個新的action。
handleUnKnownResult內部實現
String location = "/unknowresult.jsp"; return new ServletDispatcherResult(location);
若是action處理了,結果爲爲未知類型。
出問題的基本都是開發人員在開發過程當中的編碼不仔細的問題。
此類問題開發階段就要被扼殺。
在struts.xml
中用bean標籤
配置加載此實現的對象
<bean name="handler" type="com.opensymphony.xwork2.UnknownHandler" class="org.itheima.struts.handler.MyunkownHandler"></bean>
unkown-hanlder-ref標籤
註冊此bean <unknown-handler-stack> <unknown-handler-ref name="handler" /> </unknown-handler-stack>
當action代碼執行中出現異常時,錯誤信息會反饋到客戶頁面,用戶體驗很差
action標籤
中配置exception-mapping標籤
1 2 3<result name="xxx" type="redirect">/xxx.jsp</result> <exception-mapping result="xxx" exception="java.lang.Throwable" />
exception-mapping標籤
中的result屬性
指向捕獲異常後出錯的頁面結果。exception-mapping標籤
中的exception屬性
指的是要捕獲那種異常。package標籤
中配置global-exception-mapping標籤
<global-results> <result name="xxx">xxx.jsp</result> </global-results> <global-exception-mappings> <exception-mapping result="xxx" exception="java.lang.Throwable"> </exception-mapping> </global-exception-mappings>
global-exception-mapping標籤
中的result屬性
指向捕獲異常後出錯的頁面結果。global-exception-mapping標籤
中的exception屬性
指的是要捕獲那種異常。異常捕獲的做用是爲了提示用戶界面的顯示結果,但若是出錯了,開發人員是要知道錯誤日誌的。
若是要開啓錯誤日誌,在action標籤
中配置以下:
<interceptor-ref name="defaultStack"> <param name="exception.logEnabled">true</param> <param name="exception.logLevel">error</param> </interceptor-ref>