常用的Web元素有:request、session、application等,而我們一般使用session較多,Struts2如何訪問web元素呢?這個是非常重要的內容,因為它能完成程序后臺和用戶的數據交互,下面以注冊為例演示其過程:
1、index.jsp文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<%@ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > <base href= "<%=basePath %>" /> <title>Insert title here</title> </head> <body> <h1>演示</h1> <form action= "user/user02!register" method= "post" > 姓名:<input type= "text" name= "user.name" ></input> <br/> 密碼:<input type= "text" name= "user.password" ></input> <br/> <input type= "submit" value= "注冊" /> </form> </body> </html> |
功能很簡單--即用戶輸入用戶名和密碼,然后后臺可以獲得,然后注冊成功后顯示給用戶
2、struts.xml 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> < struts > < constant name = "struts.devMode" value = "true" /> < package name = "front" namespace = "/user" extends = "struts-default" > < action name = "user*" class = "com.myservice.web.UserAction{1}" > < result >/success.jsp</ result > < result name = "error" >/error.jsp</ result > </ action > </ package > </ struts > |
可以有兩種方式完成這個功能
3、第一種(UserAction01)
1
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
|
package com.myservice.web; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class UserAction01 extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private User user; private Map request; private Map session; private Map application; public UserAction01(){ request = (Map)ActionContext.getContext().get( "request" ); session = ActionContext.getContext().getSession(); application = ActionContext.getContext().getApplication(); } public String register(){ request.put( "name" , user.getName()); request.put( "password" , user.getPassword()); return SUCCESS; } public User getUser() { return user; } public void setUser(User user) { this .user = user; } } |
這個方式是用ActionContext.getContext()方法獲得context,然后得到request和session以及application
4、另外一種方式(UserAction02)非常常見,也是非常著名的方式-----Ioc(控制反轉)和DI(依賴注入),它需要實現3個接口如下:
1
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
|
package com.myservice.web; import java.util.Map; import org.apache.struts2.interceptor.ApplicationAware; import org.apache.struts2.interceptor.RequestAware; import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionSupport; public class UserAction02 extends ActionSupport implements RequestAware, SessionAware,ApplicationAware{ private Map<String, Object> request; private Map<String, Object> session; private Map<String, Object> application; private User user; public User getUser() { return user; } public void setUser(User user) { this .user = user; } public String register(){ request.put( "name" , user.getName()); request.put( "password" , user.getPassword()); return SUCCESS; } @Override public void setApplication(Map<String, Object> application) { // TODO Auto-generated method stub this .application = application; } @Override public void setSession(Map<String, Object> session) { // TODO Auto-generated method stub this .session = session; } @Override public void setRequest(Map<String, Object> request) { // TODO Auto-generated method stub this .request = request; } } |
這樣就實現了一個功能--將user的名稱和密碼都放入request中,在使用時我們只需取出即可
5、success.jsp將request中內容取出并顯示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> < html > < head > < meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" > < title >Insert title here</ title > </ head > < body > < h3 >成功注冊</ h3 > < s:property value = "#request.name" />注冊成功,密碼為:< s:property value = "#request.password" /> </ body > </ html > |
其結果顯示為:
以上就是Struts2中訪問Web元素的全部內容,希望能給大家一個參考,也希望大家多多支持服務器之家。