新手,刚学习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)
万分感谢!