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

request.getAttribute

时间:2017-07-13 01:03:47      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:head   ext   work   容器   aof   mit   user   反向   演示   

1、在Struts2中,通常是先进入jsp页面,再进入action中;如果要实现action页面所得信息的反向输入到上述的jsp页面中,就可以使用request.getAttribute();

2、request.setAttribute()和getAttribute()方法传递的数据只会存在于Web容器内部

3、部分代码演示

  (1)jsp代码

 1 <html>
 2   <head>      
 3   <title></title>
 4   </head>
 5   
 6   <body>
 7     <h3>用户登录</h3>
 8     <hr/>
 9     
10     <form action="Login.action" method="post">
11         <table>
12             <tr>
13               <td style="width:60px;"><label for="username$text">身份:</label></td>
14               <td>
15                   <select name="Type" id="Type" style="width:148px;"> 
16                     <option value="请选择">请选择</option>
17                     <option value="系统管理员">系统管理员</option>
18                     <option value="楼宇管理员">楼宇管理员</option>
19                     <option value="学生">学生</option>
20                   </select>
21               </td>
22             </tr>
23             <tr>
24                 <td>用户名</td>
25                 <td><input type="text" name="Username" onvalidation="onUserNameValidation" 
26                             class="mini-textbox"  
27                             required style="width:150px;"></td>
28             </tr>
29             <tr>
30                 <td>密码</td>
31                 <td><input type="password" name="Password" onvalidation="onUserNameValidation" 
32                             class="mini-textbox" 
33                             required style="width:150px;"></td>
34             </tr>
35             <tr>
36                 <td colspan="2">
37                     <input type="submit" value="登录">&nbsp;&nbsp;
38                     <input type="reset" value="重置">
39                 </td>
40             </tr>
41             <%if(request.getAttribute("Msg")!=null){%>
42             <%=request.getAttribute("Msg")%>
43             <%}%>
44         </table>
45     </form>
46   </body>
47 </html>

  (2)action代码

 1 package com.action;
 2 
 3 import com.factory.StudentDAOFactory;
 4 import com.opensymphony.xwork2.ActionContext;
 5 import com.vo.Student;
 6 
 7 public class Login {
 8     private String Type;
 9     private String Username;
10     private String Password;
11     private String Msg;
12     public String getType() {
13         return Type;
14     }
15     public void setType(String type) {
16         Type = type;
17     }
18     public String getUsername() {
19         return Username;
20     }
21     public void setUsername(String username) {
22         Username = username;
23     }
24     public String getPassword() {
25         return Password;
26     }
27     public void setPassword(String password) {
28         Password = password;
29     }
30     public String getMsg() {
31         return Msg;
32     }
33     public void setMsg(String msg) {
34         Msg = msg;
35     }
36     public String execute()throws Exception{
37         if(Type.equals("学生")){
38             Student student = new Student();
39             student.setStudent_Number(getUsername());
40             student.setStudent_Password(getPassword());
41             if(StudentDAOFactory.getIStudentDAOInstance().studentLogin(student)){
42                 ActionContext.getContext().getSession().put("student_number", student.getStudent_Number());
43                 ActionContext.getContext().getSession().put("student_name", student.getStudent_Name());
44                 ActionContext.getContext().getSession().put("student_sex", student.getStudent_Sex());
45                 ActionContext.getContext().getSession().put("student_domitory", student.getStudent_Domitory());
46                 ActionContext.getContext().getSession().put("student_password", student.getStudent_Password());
47                 ActionContext.getContext().getSession().put("student_absenceDate", student.getStudent_AbsenceDate());
48                 return "success";
49             }else{
50                 Msg = "用户名或者密码错误";
51                 return "error";
52             }
53         }else{
54             Msg = "身份选择错误!";
55             return "error";
56         }
57     }
58 }

 

request.getAttribute

标签:head   ext   work   容器   aof   mit   user   反向   演示   

原文地址:http://www.cnblogs.com/XuGuobao/p/7158018.html

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