MyEclipse搭建struts框架,并且实现登陆流程和拦截器使用(2)

上一篇介绍了,搭建struts框架和如何创建一个登陆流程,登陆成功跳转到欢迎页面,登陆失败跳转到登陆页面。

现在有这么一个需求,用拦截器实现,如果用户名为null,提示用户,用户名不能为空(此处用拦截器实现),然后返回登陆页面,如果用户名不为null ,再进入action的login方法进行下一步操作。

具体代码如下:

首先建立login.jsp


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
  </head>
   <body>
     <font color="red" size="28">提示信息: ${tip}</font>
     <form action="login.action" method="post">
         用户名:<input name="username" id="username"/>
   密 码:<input name="password" id="password"/>  
         <input type="submit" value="登陆"/>   
     </form>
  </body>
</html>


第二部 建立welcome.jsp 登陆成功时跳转的界面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
  <head>  
       <title> yuewei's Welcome</title>  
      <!-- 
  <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> 
       -->  
</head>  
<body>  
<h1>登录成功后显示此页面 ,欢迎你,${user} 你的密码是${password} </h1>  
</body>  
</html>  


第三部 ,建立拦截器

package com.cn;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LoginInterceptor extends AbstractInterceptor implements
ServletRequestAware {
@Override
public String intercept(ActionInvocation arg0) throws Exception {
Map request = (Map)ActionContext.getContext().get("request");
   String username=(String) request.get("username");
   if(username.equals("")){
    request.put("tip", "大哥,用户名不能为null");
    return "input";
   }
return arg0.invoke();
}
@Override
public void setServletRequest(HttpServletRequest arg0) {
// TODO Auto-generated method stub
}
}


第四部 建立LoginAction.java

package com.cn;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
/**

*/
private static final long serialVersionUID = 1L;
private String username;
private String password;


public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
private boolean isInvalid(String value) {
return (value == null || value.length() == 0);
}
public String execute() {
if (isInvalid(getUsername()))
return INPUT;
if (isInvalid(getPassword()))
return INPUT;
if (this.getUsername().equals("wangyonglin1123")
&& this.getPassword().equals("wangyonglin1123")||this.getUsername().equals("wangyonglinadmin")
&& this.getPassword().equals("wangyonglinadmin")) {
      ActionContext.getContext().getSession().put("user",getUsername());  
      ActionContext.getContext().getSession().put("password",getPassword());  
      return "success";
}else{
     Map request = (Map)ActionContext.getContext().get("request");
     request.put("tip", "大哥,用户名和密码输入错误!");
}
return "error";
}
}


第五步 ,配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="authority" extends="struts-default">
<interceptors>
<interceptor name="loginInterceptor" class="com.cn.LoginInterceptor"></interceptor>
<!-- 拦截器栈 -->
<interceptor-stack name="mydefault">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="loginInterceptor" />
</interceptor-stack>
</interceptors>

<global-results>
<result name="input">/login.jsp</result>
</global-results>


<action name="login" class="com.cn.LoginAction">
<result name="success">/welcome.jsp</result>
<result name="error">/login.jsp</result>
<interceptor-ref name="mydefault"></interceptor-ref>
</action>
</package>
</struts>  


第六部,发布,启动服务器,访问,当用户名为null时  ,提示用户。

  
 


已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页