作业八

在这里插入图片描述
** userDaoImpl**

package lu.user.dao.Impl;

import lu.user.dao.userDao;
import lu.user.pojo.User;
import lu.user.utils.JDBCUtils;
import org.springframework.stereotype.Repository;

import java.sql.*;
@Repository
public class userDaoImpl implements userDao{

    @Override
    public User getUser(String username, String password)  throws Exception{
        User user = null;
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCUtils.getConnection();
            String sql = "select * from smbms_user where userName = '"+username+"' and userPassword= '"+password+"'";
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(resultSet!=null){
            while (resultSet.next()){
                user=new User();
                user.setUserName(resultSet.getString("userName"));
                user.setUserPassword(resultSet.getString("userCode"));
            }
        }

        JDBCUtils.connectionClose(resultSet,statement,connection);
        return user;
    }
}

UserServiceImpl

package lu.user.service.Impl;



import lu.user.dao.Impl.userDaoImpl;
import lu.user.pojo.User;
import lu.user.service.userService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
@Service
public class userServiceImpl implements userService {

    public userDaoImpl getUserDao() {
        return userDao;
    }

    public void setUserDao(userDaoImpl userDao) {
        this.userDao = userDao;
    }

    @Resource
    private userDaoImpl userDao;

    @Override
    public User findUser(String username, String password) {
        User user =null;
        try {
            user = userDao.getUser(username,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }
}

UserController

package lu.user.controller;

import lu.user.pojo.User;
import lu.user.service.userService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    userService us=null;

    //登录
    @RequestMapping(value="/login.html",method= RequestMethod.POST)
    public String dologin(String username, String password, HttpServletRequest request, HttpSession session){
      //调用业务逻辑层
        User user=us.findUser(username, password);
        if(user==null){
			/*request.setAttribute("errors", "用户名密码不正确");*/
            throw new RuntimeException("用户名密码不正确....");
        }else{
            System.out.println("控制器user用户的值:"+user.getUserName()+user.getUserPassword());
            session.setAttribute("currentUser", user);
            //return "main";
            return "redirect:/user/main.html";
        }
    }

    //定义跳转到主页面的处理器
    @RequestMapping(value="/main.html")
    public String doMain(){
        return "main";
    }

  /*  @RequestMapping(value="/error.html")
    public String doError(){
        return "error";
    }
*/
}

在这里插入图片描述
SpringMvc-servlet

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">


<!--配置springmvc的视图解析器-->
    <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--前置解析-->
        <property name="prefix" value="/"/><!--斜杠代表直接在web目录下的-->
        <!--后置解析-->
        <property name="suffix" value=".jsp"/><!--代表以后缀名为.jsp结束的文件-->
    </bean>
    <!--补充mvc的配置  用在有map中的情况下-->
    <mvc:annotation-driven />
    <!-- 增加对控制器所在包的扫描 -->
    <context:component-scan base-package="lu.user.controller"></context:component-scan>

    <!-- 全局异常 -->
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="java.lang.RuntimeException">
                    error
                </prop>
            </props>
        </property>
    </bean>
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值