WEB开发网
开发学院软件开发Java SSH笔记四 整合struts2 阅读

SSH笔记四 整合struts2

 2009-09-23 00:00:00 来源:WEB开发网   
核心提示: 4. 配置 user.xml<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConf

4.  配置 user.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts> 
    
    <package name="user" namespace="/user" extends="struts-default">    
        <action name="login" class="userAction" method="login">
            <result name="input">/login.jsp</result>
            <result name="success" type="redirect">/welcome.jsp</result>
        </action> 
    </package>
    
    
</struts>

5.  添加UserAction和UserManager类

package net.selitech.ssim.web.user;
import java.util.Map;
import net.selitech.ssim.dao.SsimUser;
import net.selitech.ssim.user.UserManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * @author: juxuejian
 * @filename: UserAction.java
 * @date: Aug 10, 2009 , 11:10:47 PM
 * @description :
 */
public class UserAction extends ActionSupport{
    SsimUser user;
    UserManager userManager;
    
    public String login(){
        if(getSession("loginedUser")!=null) return SUCCESS;
        
        try {
            if (getUserManager().checkLogin(user.getUserEmail(), user.getUserPassword())) {
                
                // 更新登录次数
                user = getUserManager().findByEmail(user.getUserEmail());
                // 修正手工插入数据后导致int为null的问题
                
                
                // 将登录用户信息存入 session
                setSession("username", user.getUserRealname());
                setSession("loginedUser", user);
                
                
                return SUCCESS;
            } else {
            }
        }catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return INPUT;
    }
    
    public SsimUser getSessionLoginedUser() {
        SsimUser currentUser = (SsimUser) getSession("loginedUser");
        return currentUser;
    }
    
    /**
     * 读取 session 中的属性值
     * @param name
     * @return
     */
    public static Object getSession(String name) {
        ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
        return session.get(name);
    }
    
    /**
     * 向 session 设置属性值
     * @param name
     * @param value
     */
    public static void setSession(Object name, Object value) {
        ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
        session.put(name, value);
    }
    public UserManager getUserManager() {
        return userManager;
    }
    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }
    
}

package net.selitech.ssim.user;
import java.util.List;
import net.selitech.ssim.dao.SsimUser;
import net.selitech.ssim.dao.SsimUserDAO;
/**
 * @author: juxuejian
 * @filename: UserManager.java
 * @date: Aug 10, 2009 , 11:10:29 PM
 * @description :
 */
public class UserManager {
    SsimUserDAO ssimUserDAO;
    
    public boolean checkLogin(String userEmail,String password) throws Exception{
        // 是否存在给定用户
        SsimUser user = findByEmail(userEmail);
        if (user == null) {
            //用户不存在
            return false;
        }
        if(user != null && user.getUserPassword() !=  null && user.getUserPassword().equals(password)) {
            return true;
        }
        return false;
    }
    
    @SuppressWarnings("unchecked")
    public SsimUser findByEmail(String userEmail){
        List<SsimUser> users=ssimUserDAO.findByUserEmail(userEmail);
        if(users != null && users.size() > 0) {
             return users.get(0);
         }         
         return null;
    }
    public SsimUserDAO getSsimUserDAO() {
        return ssimUserDAO;
    }
    public void setSsimUserDAO(SsimUserDAO ssimUserDAO) {
        this.ssimUserDAO = ssimUserDAO;
    }
}

上一页  1 2 3 4 5  下一页

Tags:SSH 笔记 整合

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接