码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2框架02 消息传递

时间:2017-07-03 22:26:11      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:config   配置文件   开发   override   多个   tle   space   ebe   ati   

 

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 <%@taglib 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 <%@taglib 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 消息传递

标签:config   配置文件   开发   override   多个   tle   space   ebe   ati   

原文地址:http://www.cnblogs.com/NeverCtrl-C/p/7112950.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!