exa1
2021-12-26 18:34
采纳率: 40%
浏览 63
已结题

为什么账号对密码错能进入登陆失败提示页,账号错却会直接报错?

如图

img

img

img

img

这是数据库里存的账号 只有这一条

img

login.jsp

<!-- JSP指令 page声明 -->
<%@ page contentType="text/html;charset=utf-8" %>

<html>
    <head>
        <title>图书管理系统 登录</title>
        <style type="text/css">
            *{
                margin:0;
                padding:0;
            }
            body{
                background-image: url(img/bg3.png);
                background-size: 100% 100%;
            }
            #title{
                height:44px;
                margin-top:20px;
            }
            #box1{
                /*border:2px dashed black;*/
                height:240px;
                width:500px;
                text-align:center;
                border-radius:5px;
                background-color: rgba(255, 255, 255, 0.3);
                position: absolute;
                margin: auto;
                top: 0;
                left: 0;
                right: 0;
                bottom: 0;
                box-shadow:5px 5px 5px dimgray;
            }
            .in{
                width:200px;
                height:28px;
                margin-bottom:16px;
            }
            #in1{
                margin-top:7px;
            }
            .btn{
                width:100px;
                height:28px;
            }
            #btn1{
                margin-right:48px;
            }
            #form1{
                margin-top:28px;
            }
        </style>
    </head>
    <body>
        <div id="box1">
            <h1 id="title">图书管理系统</h1>
            <form id="form1" action="login.action" method="post">
                账号: <input class="in" id="in1" type="text" name="uname"/><br/>
                密码: <input class="in" type="password" name="pwd"/><br/>
                <input class="btn" id="btn1" type="submit" value=" 登 录 "/>
                <input class="btn" type="button" value=" 注 册 " onclick = "location.href = ('register.jsp')"/>
            </form>
        </div>
    </body>
</html>


loginController.java


```java
package com.cyk.springmvc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cyk.springmvc.pojo.User;
import com.cyk.springmvc.service.UserService;
import com.sun.mail.iap.Response;

@Controller
public class LoginController {
    @Autowired(required = false)
    private UserService userServ;
    @RequestMapping("/login")
    public String login(User user){
        System.out.println("login loading...");
        User rs = userServ.login(user.getUname(), user.getPwd());
        if(rs.getUname() != "" && rs.getPwd().equals(user.getPwd())){
            System.out.println(rs.toString());
            return "index";
        }else{
            return "/WEB-INF/login/fail";
        }
    }
    
    @RequestMapping("/register")
    public String Register(User user){
        System.out.println("Register loading...");
        User rs = userServ.register(user.getUname(), user.getPwd());
        if(rs.getUname() != "" && rs.getPwd() != ""){
            System.out.println(rs.toString());
            return "login";
        }else{
            return "/WEB-INF/login/fail";
        }
    }
}


service


```java
package com.cyk.springmvc.service;

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

import com.cyk.springmvc.dao.UserDAO;
import com.cyk.springmvc.pojo.User;

@Service
public class UserServiceImpl implements UserService{
    @Autowired(required = false)
    private UserDAO userDAO;
    @Override
    public User login(String uname, String pwd) {
        System.out.println("login userBiz...");
        return userDAO.getUser(uname, pwd);
    }
    
    @Override
    public User register(String uname, String pwd) {
        System.out.println("register userBiz...");
        return userDAO.setUser(uname, pwd);
    }
    
}


dao

package com.cyk.springmvc.dao;

import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.cyk.springmvc.pojo.User;

@Repository("userDAO")
public class UserDAOImpl implements UserDAO{
    static SqlSession session = null;
    public static void setup(){
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("sqlMapConfig.xml");
        } catch (Exception e) {
            e.printStackTrace();
        }
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        session = factory.openSession();
    }
    
    public static void tearDown(){
        session.commit();
    }
    
    @Override
    public User getUser(String uname, String pwd) {
        System.out.println("login userDao...");
        setup();
        User user = session.selectOne("com.cyk.springmvc.pojo.getUserByUname", uname);
        tearDown();
        return user;
    }

    @Override
    public User setUser(String uname, String pwd) {
        System.out.println("register userDao...");
        User user01 = new User();
        user01.setUname(uname);
        user01.setPwd(pwd);
        setup();
        session.insert("com.cyk.springmvc.pojo.addUser", user01);
        tearDown();
        return getUser(uname, pwd);
    }
}


springmvc


```xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <context:component-scan base-package="com.cyk.springmvc"></context:component-scan>
  <!--     <mvc:annotation-driven/> -->

    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--前缀 -->
        <!--<property name="prefix" value="/WEB-INF/" />-->
        <!--后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

sqlMapConfig

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="hello_mybatis">
        <environment id="hello_mybatis">
            <transactionManager type="jdbc"/>
            <dataSource type="pooled">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/userdb?characterEncoding=utf8"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    
    <mappers>
        <mapper resource="com/cyk/springmvc/pojo/UserMapper.xml"/>
    </mappers>
    
</configuration>

userMapper

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.cyk.springmvc.pojo">
    <insert id="addUser" parameterType="com.cyk.springmvc.pojo.User">
        INSERT INTO t_user VALUES(#{uname}, #{pwd})
    </insert>
    <select id="getUserByUname" resultType="com.cyk.springmvc.pojo.User" parameterType="string">
        SELECT * FROM t_user WHERE uname = #{uname}
    </select>
</mapper>


代码如上,希望各位帮忙看看,谢谢了

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

相关推荐 更多相似问题