【JavaEE企业应用实战学习记录】struts2实现登录并获取各个范围的数据

 1 package sanglp;
 2 
 3 import com.opensymphony.xwork2.*;
 4 
 5 /**
 6  * Created by Administrator on 2016/10/6.
 7  */
 8 public class LoginAction implements com.opensymphony.xwork2.Action {
 9 
10     //定义封装请求参数的username和password属性
11     private String username;
12     private String password;
13 
14     //定义处理用户请求的execute方法
15     public String execute(){
16         /*if(getUsername().equals("crazyit.org")&&getPassword().equals("leegang")){
17             ActionContext.getContext().getSession().put("user",getUsername());
18             return SUCCESS;
19         }else{
20             return ERROR;
21         }*/
22         ActionContext ctx=ActionContext.getContext();
23         //通过ActionContext访问application范围的属性值
24         Integer counter=(Integer)ctx.getApplication().get("counter");
25         if(counter==null){
26             counter=1;
27         }else{
28             counter=counter+1;
29         }
30         //通过ActionContext设置application范围的属性
31         ctx.getApplication().put("counter",counter);
32         //通过ActionContext设置session范围的属性
33         ctx.getSession().put("user",getUsername());
34         if(getUsername().equals("crazyit.org")&&getPassword().equals("leegang")){
35             //通过ActionContext设置request范围的属性
36             ctx.put("tip","服务器提示,您已经成功的登录");
37             return SUCCESS;
38         }else{
39             ctx.put("tip","服务器提示,登录失败");
40             return ERROR;
41         }
42     }
43     public String getUsername() {
44         return username;
45     }
46 
47     public String getPassword() {
48         return password;
49     }
50 
51     public void setUsername(String username) {
52         this.username = username;
53     }
54 
55     public void setPassword(String password) {
56         this.password = password;
57     }
58 }
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"%>
 2 <html>
 3 <!--welcome.jsp-->
 4 <head>
 5 </head>
 6 <body>
 7 本站访问次数为:${applicationScope.counter}<br/>
 8 ${sessionScope.user},您已经登录<br/>
 9 ${requestScope.tip}
10 </body>
11 </html>
 1 <%--
 2 login.jsp
 3   Created by IntelliJ IDEA.
 4   User: Administrator
 5   Date: 2016/10/6
 6   Time: 16:26
 7   To change this template use File | Settings | File Templates.
 8 --%>
 9 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
10 <%@taglib prefix="s" uri="/struts-tags" %>
11 <html>
12 <head>
13     <title><s:text name="loginPage"/>></title>
14 </head>
15 <body>
16 <s:form action="login">
17     <s:textfield name="username" key="user"/>
18     <s:textfield name="password" key="pass"/>
19     <s:submit key="login"/>
20 </s:form>
21 </body>
22 </html>
1  <package name="sanglp" extends="struts-default">
2         <action name="login" class="sanglp.LoginAction">
3             <!--定义三个逻辑视图和物理资源之间的映射-->
4             <result name="input">/login.jsp</result>
5             <result name="error">/error.jsp</result>
6             <result name="success">/welcome.jsp</result>
7         </action>
8     </package>

posted @ 2016-10-06 21:04  霓裳梦竹  阅读(297)  评论(0)    收藏  举报