JavaWeb_(Struts2框架)使用Struts框架实现用户的登陆

 

 

  JavaWeb_(Struts2框架)使用Servlet实现用户的登陆  传送门

  JavaWeb_(Struts2框架)Servlet与Struts区别  传送门

 

  MySQL数据库中存在Gary用户,密码为123;第一次登陆时输入错误的密码1234后页面重定向,并输出错误的提示信息第二次登陆时输入正确的密码,页面跳转到index.jsp中

 

 

  c3p0-config.xml链接本地数据库

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>

    <default-config>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql:///strutstest</property>
        <property name="user">root</property>
        <property name="password">123456</property>
        <property name="initialPoolSize">5</property>
        <property name="maxPoolSize">20</property>
    </default-config>




</c3p0-config>
c3p0-config.xml

 

  struts.xml配置参数的传递和请求的转发

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
    
    
<struts>
    <!-- name:配置包名 -->
    <package name="MyPackage" namespace="/" extends="struts-default">
        <action name="LoginAction" class="com.Gary.web.UserAction" method="execute">
            <!-- 默认为转发  redirect设置为重定向-->
            <result name="success" type="redirect">/index.html</result>
            <result name="error">/login.jsp</result>
        </action>
    </package>
</struts>
struts.xml

 

package com.Gary.domain;

public class User {

    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
    
    
}
User.java

 

package com.Gary.service;

import java.sql.SQLException;

import com.Gary.dao.UserDao;
import com.Gary.domain.User;

public class UserService {

    public boolean findUser(User user) throws SQLException {
        UserDao userDao = new UserDao();
        User temp = userDao.findUser(user);
        return temp ==null?false:true;
    }

}
UserService.java

 

package com.Gary.web;

import org.apache.struts2.ServletActionContext;

import com.Gary.domain.User;
import com.Gary.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<User>{

    public User user = new User();
    
    public String execute() throws Exception {
        
        System.err.println("我已经运行了");
        
        UserService userService = new UserService();
        boolean success = userService.findUser(user);
        if(success)
        {
            return "success";
        }else{
            ServletActionContext.getRequest().setAttribute("error", "用户名或密码错误!!!");
            return "error";
        }
        
    }

    
    @Override
    public User getModel() {
        // TODO Auto-generated method stub
        return user;
    }
}
UserAction.java

 

package com.Gary.dao;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.Gary.domain.User;
import com.yl.lain.utils.C3p0DataSourceUtils;

public class UserDao {

    public User findUser(User user) throws SQLException {
        QueryRunner runner = new QueryRunner(C3p0DataSourceUtils.getDataSource());
        String sql = "select * from user where username = ? and password = ?";
        return runner.query(sql, new BeanHandler<User>(User.class),user.getUsername(),user.getPassword());
    
    }

}
UserDao.java

 

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">

<link rel="stylesheet" href="css/head.css" />
<link rel="stylesheet" type="text/css" href="css/login.css" />
</head>

<body>
    <div class="dvhead">
        <div class="dvlogo">
            <a href="index.html">你问我答</a>
        </div>
        <div class="dvsearch">10秒钟注册账号,找到你的同学</div>
        <div class="dvreg">
            已有账号,立即&nbsp;<a href="login.html">登录</a>
        </div>
    </div>
    <section class="sec">
        <form action="${pageContext.request.contextPath }/LoginAction" method="post">
            <div class="register-box">
                <label for="username" class="username_label"> 用 户 名 <input maxlength="20" name="username" type="text" placeholder="您的用户名和登录名" />
                </label>
                <div class="tips"></div>
            </div>
            <div class="register-box">
                <label for="username" class="other_label"> 设 置 密 码 <input maxlength="20" type="password" name="password" placeholder="建议至少使用两种字符组合" />
                </label>
                <div class="tips"></div>
            </div>
            <div class="arguement">
                <input type="checkbox" id="xieyi" /> 阅读并同意 <a href="javascript:void(0)">《你问我答用户注册协议》</a> <a href="register.html">没有账号,立即注册</a>
                <div class="tips" style="color: red">${error}</div>
            </div>
            <div class="submit_btn">
                <button type="submit" id="submit_btn">立 即 登录</button>
            </div>
        </form>
    </section>
    <script src="js/index.js" type="text/javascript" charset="utf-8"></script>
</body>
login.jsp

 

 

  项目结构

  

 

  数据库中用户数据

   

 

 

 实现过程

   通过c3p0-config.xml链接本地数据库

    <default-config>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql:///strutstest</property>
        <property name="user">root</property>
        <property name="password">123456</property>
        <property name="initialPoolSize">5</property>
        <property name="maxPoolSize">20</property>
    </default-config>

 

   web层接受由login.jsp页面的表单/login请求

<form action="${pageContext.request.contextPath }/LoginAction" method="post">
            <div class="register-box">
                <label for="username" class="username_label"> 用 户 名 <input maxlength="20" name="username" type="text" placeholder="您的用户名和登录名" />
                </label>
                <div class="tips"></div>
            </div>
            <div class="register-box">
                <label for="username" class="other_label"> 设 置 密 码 <input maxlength="20" type="password" name="password" placeholder="建议至少使用两种字符组合" />
                </label>
                <div class="tips"></div>
            </div>
            <div class="arguement">
                <input type="checkbox" id="xieyi" /> 阅读并同意 <a href="javascript:void(0)">《你问我答用户注册协议》</a> <a href="register.html">没有账号,立即注册</a>
                <div class="tips" style="color: red">${error}</div>
            </div>
            <div class="submit_btn">
                <button type="submit" id="submit_btn">立 即 登录</button>
            </div>
        </form>

 

   struts.xml处理用户登陆时的转发

<struts>
    <!-- name:配置包名 -->
    <package name="MyPackage" namespace="/" extends="struts-default">
        <action name="LoginAction" class="com.Gary.web.UserAction" method="execute">
            <!-- 默认为转发  redirect设置为重定向-->
            <result name="success" type="redirect">/index.html</result>
            <result name="error">/login.jsp</result>
        </action>
    </package>
</struts>

 

   在domain层中创建用户User实体

    private String username;
    private String password;

 

package com.Gary.domain;

public class User {

    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
    
    
}
User.java

 

  web层LoginServlet.java处理用户登陆逻辑,由servlet层去调用dao层返回数据库中查询用户信息

    public User findUser(User user) throws SQLException {
        QueryRunner runner = new QueryRunner(C3p0DataSourceUtils.getDataSource());
        String sql = "select * from user where username = ? and password = ?";
        return runner.query(sql, new BeanHandler<User>(User.class),user.getUsername(),user.getPassword());
    
    }

 

  dao层得到用户信息后传递给servlet层,servlet层获得user对象后传递给web层,web层处理用户登陆错误和登陆成功逻辑用户登陆成功返回字符串success否则返回error

  struts.xml中接受LoginAction转发,当接收到web层字符串success后将页面重定向到index.html,否则跳转到login.jsp并显示错误的提示信息(将error封装到request域中在login.jsp中使用${error}标签将信息提示出来)  

    public boolean findUser(User user) throws SQLException {
        UserDao userDao = new UserDao();
        User temp = userDao.findUser(user);
        return temp ==null?false:true;
    }

 

        <action name="LoginAction" class="com.Gary.web.UserAction" method="execute">
            <!-- 默认为转发  redirect设置为重定向-->
            <result name="success" type="redirect">/index.html</result>
            <result name="error">/login.jsp</result>
        </action>

 

package com.Gary.service;

import java.sql.SQLException;

import com.Gary.dao.UserDao;
import com.Gary.domain.User;

public class UserService {

    public boolean findUser(User user) throws SQLException {
        UserDao userDao = new UserDao();
        User temp = userDao.findUser(user);
        return temp ==null?false:true;
    }

}
UserService.java

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
    
    
<struts>
    <!-- name:配置包名 -->
    <package name="MyPackage" namespace="/" extends="struts-default">
        <action name="LoginAction" class="com.Gary.web.UserAction" method="execute">
            <!-- 默认为转发  redirect设置为重定向-->
            <result name="success" type="redirect">/index.html</result>
            <result name="error">/login.jsp</result>
        </action>
    </package>
</struts>
struts.xml

 

posted @ 2019-03-02 23:34  Cynical丶Gary  阅读(510)  评论(0编辑  收藏  举报