1. 程式人生 > >Struts2登入及其攔截器的實現(Struts2+Hibernate)

Struts2登入及其攔截器的實現(Struts2+Hibernate)

1 登入功能的簡單實現

1.1 Action類

package com.itykd.web.action;

import org.apache.struts2.ServletActionContext;

import com.itykd.domain.User;
import com.itykd.service.UserService;
import com.itykd.service.impl.UserServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class LoginAction extends ActionSupport implements ModelDriven<User>{
	private User user = new User();
	@Override
	public User getModel() {
		return user;
	}
	public String login() {
		UserService userService = new UserServiceImpl();
		User existUser = userService.login(user);
		if(existUser == null){
			this.addActionError("使用者名稱或密碼錯誤");
			return LOGIN;
		}else{
			ServletActionContext.getRequest().getSession().setAttribute("existUser", existUser);
			return SUCCESS;
		}
	}

}

1.2  struts.xml的配置

<struts>
	<package name="demo" extends="struts-default" namespace="/">
		<action name="login_*" class="com.itykd.web.action.LoginAction" method="{1}">
		    <result name="success" type="redirect">/success.jsp</result>
		    <result name="login">/loginTest.jsp</result>
		</action>
</struts>

1.3 jsp頁面的編寫 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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>
    <s:actionerror/>
	<form action="${pageContext.request.contextPath }/login_login.action" method="post">
	        使用者名稱 :<input type="text" name="user_code" /><br/> 
	        密碼:<input type="password" name="user_password"/><br/> 
	   <input type="submit" value="提交"/>
	</form>
</body>
</html>

 1.4 service層編寫

package com.itykd.service.impl;

import com.itykd.dao.UserDao;
import com.itykd.dao.impl.UserDaoImpl;
import com.itykd.domain.User;
import com.itykd.service.UserService;

public class UserServiceImpl implements UserService {

	@Override
	public User login(User user) {
		UserDao userDao = new UserDaoImpl();
		return userDao.login(user);
	}

}

1.5 dao層編寫

package com.itykd.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.itykd.dao.UserDao;
import com.itykd.domain.User;
import com.itykd.util.HibernateUtils;

public class UserDaoImpl implements UserDao {

	@Override
	public User login(User user) {
		Session session = HibernateUtils.getCurrentSession();
		Transaction tx = session.beginTransaction();	
		Query query = session.createQuery("from User where user_code=? and user_password=?");
		query.setParameter(0, user.getUser_code());
		query.setParameter(1, user.getUser_password());
		List<User> list = query.list();
		if(list.size()>0){
			return list.get(0);
		}
		
		tx.commit();
		return null;
	}

}

2 登入攔截器的實現 

2.1 Interceptor類的編寫

package com.itykd.web.interceptor;

import org.apache.struts2.ServletActionContext;

import com.itykd.domain.User;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class PrivilegeInterceptor extends MethodFilterInterceptor{

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		//判斷session中是否存在使用者資料
		User existUser=(User) ServletActionContext.getRequest().getSession().getAttribute("existUser");
		//判斷從session中獲取的使用者的資訊是否為空
		if(existUser==null) {
			//session中沒有使用者資訊,則沒有登入
			ActionSupport actionSupport = (ActionSupport) invocation.getAction();
			//輸出提示資訊
			actionSupport.addActionError("沒有登入,沒有許可權訪問");
			//返回登入頁面
			return actionSupport.LOGIN;
		}else {
			//已經登入
			return invocation.invoke();
			
		}
		
	}

}

 2.2 攔截器的配置

<struts>
	<package name="demo" extends="struts-default" namespace="/">
    	<interceptors>
	        <interceptor name="privilegeInterceptor" class="com.itykd.web.interceptor.PrivilegeInterceptor"/>	    
	    </interceptors>
		<action name="login_*" class="com.itykd.web.action.LoginAction" method="{1}">
		    <result name="success" type="redirect">/success.jsp</result>
		    <result name="login">/loginTest.jsp</result>
          <!-- 引入攔截器 -->
			<interceptor-ref name="privilegeInterceptor"> 
		        <!-- LoginAction.login()不需要進行攔截 -->
		       <param name="execludeMethods">login</param>
		    </interceptor-ref>
		    <interceptor-ref name="defaultStack"/>
		</action>
</struts>