struts2验证用户是否登陆
struts2验证用户是否登陆
南桥北木 发表于1年前
struts2验证用户是否登陆
  • 发表于 1年前
  • 阅读 4
  • 收藏 0
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="loginaction" method="post"> <center> <h2>用户登录</h2> ${loginerror} 用户:<input type="text" name="username"><br><br>

    密码:<input type="password" name="password"><br><br>
  <input type="submit" value="提交">   
</center>

</form> </body> </html>

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="default" namespace="/" extends="struts-default"> <!-- 注册拦截器 --> <interceptors> <interceptor name="validate" class="com.validate.lan.AuthorIntercept"></interceptor> </interceptors> <action name="author"> <result>/login.jsp</result> <result name="login">/login.jsp</result>

      <!-- 引用拦截器 -->

       <interceptor-ref name="defaultStack"></interceptor-ref>

       <interceptor-ref name="validate"></interceptor-ref> 

        
   </action>
    <action name="loginaction" class="com.validate.action.LoginAction" method="login">
        <result name="success">/manager.jsp</result>
        <result name="error">/login.jsp</result>
     
      
    </action>
</package>

</struts>

3LoginAction.java

package com.validate.action;

import java.util.Map;

import javax.jms.Session;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport implements SessionAware{ private String username; public String getUsername() { return username; }

public void setUsername(String username) {
	this.username = username;
}

public String getPassword() {
	return password;
}

public void setPassword(String password) {
	this.password = password;
}

private String password;

private Map<String,Object> session;

public void setSession(Map<String, Object> session) {
	  this.session=session;
}

public String login(){
	  if("123".equals(username)&&"123".equals(password)){
		  session.put("logininfo", username);
		  return SUCCESS;
	  }else{
		  session.put("loginerror", "用户名或密码错误");
		  return ERROR;  
	  }
}

}

4 AuthorIntercept.java

package com.validate.lan;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class AuthorIntercept extends AbstractInterceptor {

@Override
public String intercept(ActionInvocation invocation) throws Exception {
	ActionContext context=ActionContext.getContext();
	Map<String,Object> session=context.getSession();
	if(session.get("logininfo")!=null){
		String result=invocation.invoke();
		return result;
	}else{
		return "login";
	}
	

}

}

5 小知识 自定义拦截器栈

<interceptors>

    <interceptor name="validate" class="com.validate.lan.AuthorIntercept"></interceptor>

    <interceptor-stack name="mystack">

        <interceptor-ref name="validate"></interceptor-ref>

        <interceptor-ref name="defaultStack"></interceptor-ref>

    </interceptor-stack>

</interceptors>
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 125
码字总数 23375
×
南桥北木
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: