登录的实现(第四天)

第四天;
总结:
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);
    }

}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 35,501评论 18赞 399
  • 一. Java基础部分.................................................
    wy_sure阅读 4,158评论 0赞 11
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 137,456评论 19赞 139
  • 都说时光太快,记忆太乱,可我们之间经久不衰,眼不见,心念。最珍贵的大抵就是如此,你不在我身边,却给我最大的温暖。你...
    追天青年阅读 467评论 0赞 0
  • 一片, 两片, 三片,青浅。 ...
    一只爱生活的羊阅读 469评论 0赞 2

友情链接更多精彩内容