2
3 import org.apache.struts2.convention.annotation.Action;
4 import org.apache.struts2.convention.annotation.Actions;
5 import org.apache.struts2.convention.annotation.Result;
6 import org.apache.struts2.convention.annotation.Results;
7
8 import com.opensymphony.xwork2.ActionSupport;
9
10
11 /*
12 * 這里的Results是此Action類中的全局跳轉路徑
13 * 如果要配置所有的Action類都能夠用的全局跳轉路徑則在struts.xml中配置
14 * <!-- 全局results配置 -->
15 <global-results>
16 <result name="input">/error.jsp</result>
17 </global-results>
18 */
19 @Results({
20 @Result(name="index", location="/WEB-INF/page/index.jsp"),
21 @Result(name="test", location="/WEB-INF/page/test.jsp")
22 })
23 public class HomeAction extends ActionSupport {
24
25
26 public String execute() throws Exception{
27
28 //訪問數據庫
29 //獲取首頁中應該顯示的數據
30 //怎么從/--->home.action ??????
31 //home.action---->index.jsp
32 return "index";
33 }
34
35 @Action(value="/other/bar",
36 results={@Result(name = "one", location = "www.baidu.com",type="redirect"),
37 @Result(name = "two", location = "/WEB-INF/page/two.jsp")
38 })
39 public String myTest(){
40 String test="dd";
41 if("dd".equals(test)){
42 return "one";
43 }else{
44 return "two";
45 }
46 }
47
48 @Actions({
49 @Action("/different/url"),
50 @Action("/another/url")
51 })
52 public String newTest(){
53 return "three";
54 }
55
56 //使用默認的訪問路徑,但是要規定返回的頁面路徑
57 //@Results @Result不允許在方法前面使用
58 //一個Action不寫value則其默認的路徑是什么???????????
59 @Action(value="home",
60 results={@Result(name = "home",location="home.action",type="redirect")
61 })
62 public String testFour(){
63
64 return "home";
65 }
66
67 }
68
重點關注一下type=redirect,redirectAction,chain的區別

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

******************************************************************************
1》redirect:action處理完后重定向到一個視圖資源(如:jsp頁面),請求參數全部丟失,action處理結果也全部丟失。
2》redirect-action:action處理完后重定向到一個action,請求參數全部丟失,action處理結果也全部丟失。
3》chain:action處理完后轉發到一個action,請求參數全部丟失,action處理結果不會丟失。
怎么我自己實驗的請求參數沒有丟失了???? ${username},請求數據應該不會丟失
******************************************************************************
Redirect Action Result:
這個Result使用ActionMapperFactory提供的ActionMapper來重定位瀏覽器的URL來調用指定的action和(可選的)namespace.
這個Result比ServletRedirectResult要好.因為你不需要把URL編碼成xwork.xml中配置的ActionMapper提供的模式.
這就是說你可以在任意點上改變URL模式而不會影響你的應用程序. 因此強烈推薦使用這個Result而不是標準的redirect result來解決重定位到某個action的情況.
ActionName (默認) - 重定位到的action名
namespace - action的名稱空間. 如果為null,則為當前名稱空間
Redirect Result
調用{@link HttpServletResponse#sendRedirect(String) sendRedirect}方法來轉到指定的位置.
HTTP響應被告知使瀏覽器直接跳轉到指定的位置(產生客戶端的一個新請求). 這樣做的結果會使剛剛執行的action(包括action實例,action中的錯誤消息等)丟失, 不再可用.
這是因為action是建立在單線程模型基礎上的. 傳遞數據的唯一方式就是通過Session或者可以為Ognl表達式的web參數(url?name=value)
location (默認) - action執行后跳轉的地址.
parse - 默認為true. 如果設置為false, location參數不會被當作Ognl表達式解析.
<result name="success" type="redirect">/displayCart.action?userId=${userId}</result>
<action name= "delete " class= "com.zeng.action.UserManageAction " method= "delete ">
<result type= "redirect "> list.action?pageBean.pageNumber=${pageBean.pageNumber} </result>
</action>
*********************************************************************************************************
今天在用struts2在做項目時候,從一個action我想跳轉到另一個action,并且呢得帶上值。說說我的做法吧,首先你得在你的第一個action中這個id必須要有set、get方法。
跳轉時你的struts.xml:
(方法一):
<result name="topic" type="redirect">/topicAction!findTopics.do?topicId=${topicId}</result>
(方法二):
<result name="topic" type="redirect-action">
<param name="actionName">findTopics</param>
<param name="topicId">${topicId}</param>
</result>
如果是多個參數的話,繼續再加幾個<param>就行了,對于(方法一)如果是多個參數的怎么辦?
<result name="topic" type="redirect">/topicAction!findTopics.do?topicId=${topicId}&elementId=${elementId}</result>
這不就行了。
********************************************************************************
使用redirect重置鏈接需要后綴名,使用了redirect——action就不能使用了,
就例如使用chain一樣,只需要寫action的配置名,如果加入后綴名.action,就會報出異常,action未配置正確。
鍵字: struts2 redirect-action 傳遞 參數
????? 在做一個系統,使用struts2框架,在提交一個請求后,將獲取的數據對象再要生成一個序列號,為了防止刷新生成冗余序列號,就在請求處理完成后,直接重定向到顯示該信息的action中:
<action name="enterpreinfo" class="preinfoBusinessAction" method="enterPreinfoSub">
<result name="success" type="redirect-action">
showpreinfo?preinfo.order_number=${preinfo.order_number}&preinfo.company_name=${preinfo.company_name}
</result>
<result name="error" type="redirect">
<param name="location">/error.jsp</param>
</result>
</action>
?因為使用了redirect-action,所以要注意不能將showpreinf?preinfo.order_number=${preinfo.order_number}寫成showpreinf.action?preinfo.order_number=${preinfo.order_number}
在這個配置文件里,多個參數的連接符使用了"&",但XML的語法規范,應該使用"&"代替"&",原理和HTML中的轉義相同,開始沒有注意,在struts分析配置文件時,總是報出這樣的錯誤:
The reference to entity "preinfo" must end with the ';' delimiter.
?
進行上面說明的替換后,就正常了。
************************************************************************************
This is how I should do it
@Results({
@Result(name="input", type="redirectAction", params = {"actionName" , "resend"})
})
*************************************************************************************
http://corradignw.javaeye.com/blog/355717
struts2.1.6無論是xml還是annotation配置redirectAction時,如果要傳一些參數,
可是這些參數在ServletActionRedirectResult并沒有聲明,這時ognl會拋異常出來。
但實際上傳值是成功的。詳見struts2的jira:
例:
Java代碼
@Results({
@Result(name="reload",type="redirectAction"
,params={"actionName","hello_world"
,"namespace","/center/part1"
,"id","09"
,"count","90"})
})
@Results({
@Result(name="reload",type="redirectAction"
,params={"actionName","hello_world"
,"namespace","/center/part1"
,"id","09"
,"count","90"})
})
把日志級別調高也不管用,好像還沒有什么解決辦法。
****************************************************************************************
dispatcher 結果類型為缺省的result類型,用于返回一個視圖資源(如:jsp)
Xml代碼 :
<result name="success">/main.jsp</result>
<result name="success">/main.jsp</result>
以上寫法使用了兩個默認,其完整的寫法為:
<result name="success" type="dispatcher">
<param name="location">/maini.jsp</param>
</result>
location只能是頁面,不能是另一個action(可用type="chain"解決)。
redirect 結果類型用于重定向到一個頁面,另一個action或一個網址。
Xml代碼:
<result name="success" type="redirect">aaa.jsp</result>
<result name="success" type="redirect">bbb.action</result>
<result name="success" type="redirect">www.baidu.com</result>
redirect-action 結果類型使用ActionMapperFactory提供的ActionMapper來重定向請求到另外一個action
Xml代碼:
<result name="err" type="redirect-action">
<param name="actionName">重定向的Action名</param>
<param name="namespace">重定向Action所在的名字空間</param>
</result>
redirect和redirect-action兩種結果類型在使用上其實并沒有什么區別,只是寫法不同而已。
chain 用于把相關的幾個action連接起來,共同完成一個功能。
Xml代碼:
<action name="step1" class="test.Step1Action">
<result name="success" type="chain">step2.action</result>
</action>
<action name="step2" class="test.Step2Action">
<result name="success">finish.jsp</result>
</action>
處于chain中的action屬于同一個http請求,共享一個ActionContext
plaintextj 結果類型用于直接在頁面上顯示源代碼
Xml代碼:
<result name="err" type="plaintext">
<param name="location">具體的位置</param>
<param name="charSet">字符規范(如GBK)</param>
</result>