美文网首页S2SH在线答题
登录的实现(第四天)

登录的实现(第四天)

作者: setone | 来源:发表于2017-07-06 15:15 被阅读0次

第四天;
总结:
1.了解领域对象的使用
2.储存Session
3.对@ @Autowired的使用
我们现在写登录页

相比于咋天,我们多了两个文件,已经用红框标出来了
onepage.jsp是登录成功的显示页
error.jsp是登录失败的显示页
上面两个页面内容简单,无须变动
这次index.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">
    <style type="text/css">
#login {
    width: 400px;
    height: 250px;
    background-color: #f2f2f2;
    border:1px solid #DDDDDD;
    padding: 5px;
}
#login fieldset {
    border: none;
    margin-top: 10px;
}
#login fieldset legend {
    font-weight: bold;
}
#login fieldset p {
    display: block;
    height: 30px;
}
#login fieldset p label {
    display: block;
    float:left;
    text-align: right;
    font-size: 12px;
    width: 90px;
    height: 30px;
    line-height: 30px;
}
#login fieldset p input {
    display: block;
    float:left;
    border: 1px solid #999;
    width: 250px;
    height: 30px;
    line-height: 30px;
}
#login fieldset p input.code {
    width: 60px;
}
#login fieldset p img {
    margin-left: 10px;
}
#login fieldset p a {
    color: #057BD2;
    font-size: 12px;
    text-decoration: none;
    margin: 10px;
}
#login fieldset p input.btn {
    background: url("css/images/login.gif") no-repeat;
    width: 98px;
    height: 32px;
    margin-left: 60px;
    color: #ffffff;
}
#login fieldset p input.input_focus {
    background-color: #BEE7FC;
}
</style>
  </head>
  
  <body>
    <form action="answer/login.action">
    <div align="center">    
        <div id="login" >
          <fieldset>
            <legend>用户登录</legend>
            <p>
              <label>用户名:</label>
              <input name="userinfo.uame"  type="text" value="zsf"/>
            </p>
            <p>
              <label>密码:</label>
              <input name="userinfo.upsw" type="password" value="zsf"/>
            </p>
            <p>
              <label>验证码:</label>
              <input name="code" type="text" class="code" />
              ![](css/images/code.gif)<a href="#">换一张</a> </p>
            <p>
              <input name="" type="submit" class="btn"  value="登录" />
              <a href="answer/preRegister.action">注册</a><span>|</span><a href="#">忘记密码?</a> 
            </p>
          </fieldset>
        </div>
    </div>
    </form>
  </body>
</html>

struts.xml

    <package name="default" namespace="/answer" extends="struts-default">
       <action name="login" class="userinfoAction" method="login">
                <result name="success">/homepage/onepage.jsp</result>
                <result name="error">/error.jsp</result>
        </action>
    </package>

在就是我们要在appliactionContext.xml里面添加一段代码

<!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />  

以上是配置文件的变动部分
下面是java文件的变动地方

UserinfoAction.java

package com.jianshu.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.jianshu.bean.Userinfo;
import com.jianshu.service.UserinfoService;

public class UserinfoAction {
    @Autowired
    UserinfoService userinfoService;
    Userinfo userinfo;

    public Userinfo getUserinfo() {
        return userinfo;
    }

    public void setUserinfo(Userinfo userinfo) {
        this.userinfo = userinfo;
    }

    /**
     * 登录检测
     * 
     * @return
     */
    public String login() {
        List list = userinfoService.checkLongin(userinfo);
        if (list.size() > 0) {
            // 将登录人信息放入session
            Userinfo userinfo = (Userinfo) list.get(0);// 登录人的信息
            ServletActionContext.getRequest().getSession()
                    .setAttribute("userinfo", userinfo);// 把userinfo存入Session
            return "success";
        } else {
            return "error";
        }
    }

}

UserinfoService

package com.jianshu.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.jianshu.bean.Userinfo;
import com.jianshu.dao.UserinfoDAO;


public class UserinfoService {
    @Autowired
    UserinfoDAO userinfoDAO;
    /**
     * 检查是否有对应用户
     * @param userinfo
     * @return
     */
    public List checkLongin(Userinfo userinfo) {
        return userinfoDAO.findByExample(userinfo);
    }

}

相关文章

网友评论

    本文标题:登录的实现(第四天)

    本文链接:https://www.haomeiwen.com/subject/ofmhhxtx.html