基于ssh开发web项目-用户登录流程

1. pm/WebContent/admin/index.jsp

    登录后台页面:http://localhost:8888/pm/admin/index.jsp

    输入用户名和密码,JavaScript代码初步校验(checkName和checkPwd保证其至少三个字符),表单提交action,名字为login.oo,命名空间为/admin。

<script type="text/javascript">
        function sub() {
        if (checkName() && checkPwd()) {
            return true;
        }
        return false;
    }
</script>

<s:form action="login.oo" namespace="/admin" method="post"
                onsubmit="return sub();">

 

2. /pm/WebContent/WEB-INF/web.xml

    在web.xml中定义Filter来拦截用户请求。第一部分为作者自定义拦截器,区别于struts默认的拦截器struts2filter;filter-class指拦截器实现类;filter-mapping指拦截器拦截的URL地址。

    <filter>
        <filter-name>authorizefilter</filter-name>
        <filter-class>com.bbkmobile.iqoo.common.security.SecurityFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>authorizefilter</filter-name>
        <url-pattern>*.jsp</url-pattern>
        <url-pattern>*.oo</url-pattern>
    </filter-mapping>

    <filter>
<filter-name>struts2filter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

 

3. /pm/src/struts.xml

     struts.xml配置了Action和用户请求之间的对应关系。命名空间为/admin ,与之前相符;extends表示继承了default包;action以通配符定义,则表示method名和name相同均为login;该Action的实现类为adminAction;所以,对应去找命名空间为/admin的adminAction类的login方法。

<package name="admin" namespace="/admin" extends="default">
        <action name="*" class="adminAction" method="{1}">
            <result name="register">/admin/register.jsp</result>
            <result name="success">/admin/login.jsp</result>
            <result name="relogin">/admin/login.jsp</result>
            <result name="logout" type="redirect">/admin/login.jsp</result>
            <result name="login" type="redirect">/admin/test.jsp</result>
        </action>
</package>

 

4. /pm/src/admin/AdminAction.java

 public String login() throws Exception {
        try {
            if (adminService.userExists(user)) {
                adminService.richUserVO(user);
                putUserIntoSession();
                session.put("USER_PRIORITY", user.getPriority());
                session.put("USER_ID", user.getUserid());
                session.put("USER_INFO", user);
                session.put("USER_DEPID", user.getDept_id());
                String userName = user.getName();
                session.put("USER_NAME", userName);
                return "login"; // 登陆成功
            } else {
                addActionError("用户名、密码错误!");
                return "relogin";
            }
        } catch (Exception e) {
            e.printStackTrace();
            addActionError("用户名、密码错误!");
            addActionMessage("用户名、密码错误!msg");
            return "relogin"; // 登陆失败
        }
    }

 

5. /pm/src/admin/service/impl/AdminServiceImpl.java

    public boolean userExists(UserInfo user) throws Exception {
        return adminDAO.exists(user);
    }

 

6. /pm/src/admin/dao/impl/AdminDaoImpl.java

    public boolean exists(UserInfo user) throws Exception {
        if (null != findByNoAndPwd(user)) {
            return true;
        }
        return false;
    }
    public User findByNoAndPwd(UserInfo user) throws Exception {
        Session s = null;
        try {
            s = getSession();
            Query q = s
                    .createQuery("from User as po where po.employeeNo=? and po.password=?");
            q.setString(0, user.getEmployeeNo());
            q.setString(1, user.getPwd());
            List<?> ls = q.list();
            if (null != ls && ls.size() > 0) {
                return (User) ls.get(0);
            } else {
                return null;
            }
        } catch (Exception e) {
            throw e;
        } finally {
            if (null != s) {
                releaseSession(s);
            }
        }
    }

 

posted @ 2015-08-22 15:02  NightOwc  阅读(928)  评论(0编辑  收藏  举报