1. 程式人生 > >Struts2框架02 消息傳遞

Struts2框架02 消息傳遞

輸入 images 主頁 none 3.1 實體 over 代碼 out

1 消息傳遞

  瀏覽器和服務器之間的數據傳遞

2 服務器項瀏覽器發送數據

  2.1 在控制器類中增加一個屬性,該屬性的值就是服務器需要向瀏覽器發送的數據

  2.2 為該屬性增加 get 方法

  2.3 在處理請求的execute方法中為該屬性賦值

  2.4 在JSP文件中利用EL表達式獲取相關的屬性值

技術分享
 1 package cn.xiangxu.action;
 2 
 3 /**
 4  *    請求是 /demo/hello 時的處理類
 5  * @author 三少
 6  *
 7  */
 8 public class HelloAction {
 9     
10
/** 11 * 改屬性的屬性值就是服務器要傳遞給瀏覽器的數據 12 */ 13 private String message; 14 15 /** 16 * 請求處理方法 17 * @return 18 */ 19 public String execute() { 20 System.out.println("hello struts2"); 21 message = "我是從服務器發送過來的信息!"; // 給屬性賦值 22 return "success";
23 } 24 25 /** 26 * 實現屬性的get方法 27 * @return 屬性值 28 */ 29 public String getMessage() { 30 return message; 31 } 32 33 public void setMessage(String message) { 34 this.message = message; 35 } 36 37 38 }
控制器類 技術分享
 1 <%@ page language="java"
contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 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 <h2>Hello Struts2</h2> 11 <hr /> 12 <!-- 利用EL表達式獲取服務器發送過來的數據 --> 13 <h2>${message }</h2> 14 </body> 15 </html>
msg.jsp

3 瀏覽器項服務器發送數據

  3.1 基本屬性的傳遞方式(理解:個體傳遞)

    在控制器類中定義與瀏覽器頁面元素name的屬性值一致的屬性

    實現該屬性的set方法

    3.1.1 登錄案例

      》用戶輸入:http://localhost:8080/ssh01/login/toLogin

      》跳轉到登錄頁面

        技術分享

      》輸入用戶名和密碼後,點擊登錄按鈕就跳轉到主頁面

        技術分享

      》在控制臺打印用戶名和密碼

        技術分享

技術分享
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 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     <div>
11         <form action="http://localhost:8080/ssh01/login/toMain" method="post">
12             <div>
13                 用    戶:<input type="text" name="username"/>
14             </div>
15             <div>
16                 密    碼:<input type="password" name="password" />
17             </div>
18             <div>
19                 <input type="submit" value="登錄" />
20             </div>
21         </form>
22     </div>
23 </body>
24 </html>
login.jsp (登錄頁面) 技術分享
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 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     <h2>歡迎來導庠序科技主頁</h2>
11     <h2>開發人員還是菜鳥,還無法開發庠序科技的主頁面</h2>
12 </body>
13 </html>
main.jsp (主頁面) 技術分享
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <!DOCTYPE struts PUBLIC
 4     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 5     "http://struts.apache.org/dtds/struts-2.3.dtd">
 6 
 7 <struts>
 8     
 9     <!-- 註意:一個請求路徑下可以有多個請求名 -->
10     <package name="login" namespace="/login" extends="struts-default"> <!-- 請求路徑:login -->
11         <action name="toLogin"> <!-- 請求名:toLogin -->
12             <result name="success">
13             /WEB-INF/login.jsp
14             </result>
15         </action>
16         <action name="toMain" class="cn.xiangxu.action.LoginAction"> <!-- 請求名:toMain -->
17             <result name="success">
18             /WEB-INF/main.jsp
19             </result>
20         </action>
21     </package>
22     
23 </struts>
struts.xml (struts框架配置文件) 技術分享
 1 package cn.xiangxu.action;
 2 
 3 /**
 4  * 瀏覽器項服務器發送數據的控制類
 5  * @author 三少
 6  *
 7  */
 8 public class LoginAction {
 9     /**
10      * 用於接受瀏覽器發送過來的用戶名
11      */
12     private String username; // 屬性名必須和表單中的input元素的name屬性值相同,否則接收不到數據;必須實現該屬性的set方法,否則也接受不到數據
13     /**
14      * 用於接受瀏覽器發送過來的密碼
15      */
16     private String password;
17     
18     /**
19      * 請求執行方法
20      * @return
21      */
22     public String execute() {
23         System.out.println("用戶名為:" + username);
24         System.out.println("用戶密碼為:" + password);
25         return "success";
26     }
27 
28     public void setUsername(String username) {
29         this.username = username;
30     }
31 
32     public void setPassword(String password) {
33         this.password = password;
34     }
35     
36     
37 }
LoginAction (控制器類)

技術分享
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3     
 4 <[email protected] prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 5     
 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 7 <html>
 8 <head>
 9 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
10 <title>Insert title here</title>
11 </head>
12 <body>
13     <c:url var="url" value="/login/toMain"></c:url> <!-- 會自動添加前面的部分 -->
14     <div>
15         <%-- 
16         <form action="http://localhost:8080/ssh01/login/toMain" method="post">
17         --%>
18         <form action=${url } method="post"> <!-- 對上面一行代碼的優化 -->
19             <div>
20                 用    戶:<input type="text" name="username"/>
21             </div>
22             <div>
23                 密    碼:<input type="password" name="password" />
24             </div>
25             <div>
26                 <input type="submit" value="登錄" />
27             </div>
28         </form>
29     </div>
30 </body>
31 </html>
login.jsp (優化後的登錄頁面)

註意:使用 JSTL 進行的優化

  3.2 域模型傳遞方式(理解:打包傳遞)

    將瀏覽器要傳遞個服務器的數據打包成一個對象

    服務器端的控制器類增加一個對應的屬性,並且實現該屬性的set、get方法(如果只是接收數據的話無需實現get方法)

    

技術分享
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3     
 4 <[email protected] prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 5     
 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 7 <html>
 8 <head>
 9 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
10 <title>Insert title here</title>
11 </head>
12 <body>
13     <c:url var="url" value="/login2/toMain"></c:url> <!-- 會自動添加前面的部分 -->
14     <div>
15         <%-- 
16         <form action="http://localhost:8080/ssh01/login2/toMain" method="post">
17         --%>
18         <form action=${url } method="post"> <!-- 對上面一行代碼的優化 -->
19             <div>
20                 用    戶:<input type="text" name="user.username"/> <!-- 這裏的user是控制器類中的一個屬性 -->
21             </div>
22             <div>
23                 密    碼:<input type="password" name="user.password" /> <!-- 這裏的user是控制器類中的一個屬性 -->
24             </div>
25             <div>
26                 <input type="submit" value="登錄" />
27             </div>
28         </form>
29     </div>
30 </body>
31 </html>
login2.jsp (登錄頁面) 技術分享
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 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     <h2>歡迎${user.username }</h2> <!-- 獲取服務器發送過來的用戶名 -->
11     <h2>歡迎來導庠序科技主頁</h2>
12     <h2>開發人員還是菜鳥,還無法開發庠序科技的主頁面</h2>
13     
14 </body>
15 </html>
main2.jsp (主頁面) 技術分享
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <!DOCTYPE struts PUBLIC
 4     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 5     "http://struts.apache.org/dtds/struts-2.3.dtd">
 6 
 7 <struts>
 8     
 9     
10     <!-- 註意:一個請求路徑下可以有多個請求名 -->
11     <package name="login2" namespace="/login2" extends="struts-default"> <!-- 請求路徑:login -->
12         <action name="toLogin"> <!-- 請求名:toLogin -->
13             <result name="success">
14             /WEB-INF/login2.jsp
15             </result>
16         </action>
17         <action name="toMain" class="cn.xiangxu.action.LoginAction2"> <!-- 請求名:toMain -->
18             <result name="success">
19             /WEB-INF/main2.jsp
20             </result>
21         </action>
22     </package>
23     
24 </struts>
struts.xml (struts框架配置文件) 技術分享
 1 package cn.xiangxu.entity;
 2 
 3 /**
 4  * 登錄信息實體類
 5  * @author 三少
 6  *
 7  */
 8 public class User {
 9     private String username;
10     private String password;
11     
12     public String getUsername() {
13         return username;
14     }
15     public void setUsername(String username) {
16         this.username = username;
17     }
18     public String getPassword() {
19         return password;
20     }
21     public void setPassword(String password) {
22         this.password = password;
23     }
24     
25     @Override
26     public String toString() {
27         return "User [username=" + username + ", password=" + password + "]";
28     }
29     
30 }
User.java (數據實體類) 技術分享
 1 package cn.xiangxu.action;
 2 
 3 import cn.xiangxu.entity.User;
 4 
 5 /**
 6  * 瀏覽器項服務器發送數據的控制器類(利用域模型傳遞實現)
 7  * @author 三少
 8  *
 9  */
10 public class LoginAction2 {
11     /**
12      * 該屬性的屬性值就是瀏覽器發送過來的數據
13      */
14     private User user;
15     
16     public String execute() {
17         System.out.println("用戶名:" + user.getUsername());
18         System.out.println("密碼:" + user.getPassword());
19         System.out.println(user);
20         return "success";
21     }
22 
23     public User getUser() {
24         return user;
25     }
26 
27     public void setUser(User user) {
28         this.user = user;
29     }
30     
31     
32 }
LoginAction2.java (控制器類)

Struts2框架02 消息傳遞