JLQ0428 2019-02-28 17:49 采纳率: 100%
浏览 690
已采纳

springmvc写的注册登陆系统,注册可以,登陆的时候报NO DataSource specified

新手,刚学习springmvc,尝试写一个简单的注册登陆系统,发现登陆一直报错,麻烦各位大佬帮一下忙看一下,感激不尽。
JDBCOperater.java

package main.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class JDBCOperater extends NamedParameterJdbcDaoSupport {

    @Autowired
    JdbcTemplate jdbcTemplate=new JdbcTemplate();
    public void insertNamedParameter(User user){
        String sql="INSERT INTO user_t(name,password)"
                +"VALUES(:name,:password)";
        Map<String,Object> parameters=new HashMap<String,Object>();
        parameters.put("name",user.getName());
        parameters.put("password",user.getPassword());
        getNamedParameterJdbcTemplate().update(sql,parameters);
    }
          public int getMatchCode(String Name,String Password){
        String sql="SELECT count(*) from user_t where name=? and password=?";
        return  jdbcTemplate.queryForObject(sql,new Object[]{Name,Password},Integer.class);
    }
    public User findUserByUserName(final String Name){
        String sql="SELECT name,password from user_t where name=?";
        final User user=new User();
        jdbcTemplate.query(sql, new Object[]{Name}, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet resultSet) throws SQLException {
                user.setName(Name);
                user.setPassword(resultSet.getString("name"));
            }
        });
        return user;
    }
    public boolean hasMatchUser(String Name,String Password){
        int matchCount=getMatchCode(Name,Password);
        System.out.println(matchCount);
        if (matchCount>0){
            return true;
        }else {
            return false;
        }
    }
}

User.java

package main.User;

public class User  {
    private String name;
    private String password;
    private Integer id;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

UserController.java

package main.User;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.http.HttpRequest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.support.AbstractMultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.security.auth.login.LoginContext;
import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController  {
    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public ModelAndView user(){
        User user=new User();
        return new ModelAndView("user","command",user);
    }

JdbcTemplate jdbcTemplate=new JdbcTemplate();

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public ModelAndView login(){
        User user1=new User();
        return new ModelAndView("login","command",user1);
    }

    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    public String addUser(@ModelAttribute("SpringWeb")User user,Model model){
        model.addAttribute("name",user.getName());
        model.addAttribute("password",user.getPassword());
        ApplicationContext context=new ClassPathXmlApplicationContext("Spring-Database.xml");
        JDBCOperater jdbcOp=(JDBCOperater) context.getBean("user");
        jdbcOp.insertNamedParameter(user);
        //((ConfigurableApplicationContext)context).close();
        return "result";
    }
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public  String login(@ModelAttribute("SpringWeb")User user, Model model, JdbcTemplate jdbcTemplate){
        model.addAttribute("name",user.getName());
        model.addAttribute("password",user.getPassword());
        ApplicationContext context1=new ClassPathXmlApplicationContext("Spring-Database.xml");
        JDBCOperater jdbcOperater=new JDBCOperater();
        jdbcOperater.getMatchCode(user.getName(),user.getPassword());
         JDBCOperater jdbcOp=(JDBCOperater) context1.getBean("login");
        Boolean isValidUser=jdbcOperater.hasMatchUser(user.getName(),user.getPassword());
      //  System.out.println(isValidUser);
        if (!isValidUser){
            return "error";
        }else {
            return "success";

       }
    }
}

Spring-DataBase.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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/spring"/>
            <property name="username" value="root"/>
            <property name="password" value="123456"/>
        </bean>
        <bean id="user" class="main.User.JDBCOperater">
        <property name="dataSource" ref="dataSource"/>
        </bean>
    <bean id="login" class="main.User.JDBCOperater">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" abstract="false" lazy-init="false">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>

登陆界面login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>登陆</title>
</head>
<body>
<h2>登陆</h2>
<h2>用户信息</h2>
<form:form method="POST" action="/login">
    <table>
        <tr>
            <td><form:label path="name">姓名:</form:label></td>
            <td><form:input path="name"/></td>
        </tr>
        <tr>
            <td><form:label path="password">密码:</form:label></td>
            <td><form:input path="password"/></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="登陆"/></td>
        </tr>
    </table>
</form:form>
</body>
</html>

报错

java.lang.IllegalArgumentException: No DataSource specified
    at org.springframework.util.Assert.notNull(Assert.java:134)
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:97)
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:625)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:690)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:722)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:732)
    at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:800)
    at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:819)
    at main.User.JDBCOperater.getMatchCode(JDBCOperater.java:77)
    at main.User.UserController.login(UserController.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:181)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

万分感谢!

  • 写回答

4条回答 默认 最新

  • 南归北隐 博客专家认证 2019-03-01 09:34
    关注

    "SELECT name,password from spring where name

    表名叫spring?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化