request.getAttribute

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 }

 

posted @ 2017-07-12 22:40  XuGuobao  阅读(5930)  评论(0编辑  收藏  举报