我有两个控制器,一个用于通过数据库登录,另一个用于在spring java中插入数据库中的值。我有一个映射错误。如何在执行期间映射这两个控制器。错误告诉告诉它有一些参考问题。当我只运行登录控制器而不是它工作正常,但一旦我添加我的注册控制器失败。登录后实际需要注册控制器。
enter code here
package com.login.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
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.servlet.ModelAndView;
import com.login.bean.BorewellBean;
import com.login.delegate.BorewellDelegate;
@Controller
@RequestMapping(value ="/fillup")
@Component
public class BorewellController {
@Autowired
private BorewellDelegate borewelldelegate;
@RequestMapping(method=RequestMethod.GET)
public ModelAndView fillup(HttpServletRequest request,HttpServletResponse response)
{
ModelAndView model=new ModelAndView("loginsuccess");
BorewellBean bean=new BorewellBean();
model.addObject("bean",bean);
return model;
}
@RequestMapping(method=RequestMethod.POST )
public ModelAndView executefillup(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("bean")BorewellBean bean)
{
ModelAndView model=null;
try{
boolean isValidUser=borewelldelegate.setvalue(bean.getBid(),bean.getDate(),bean.getStart(),bean.getStop());
if(isValidUser)
{
System.out.println("entry done");
request.setAttribute("bid",bean.getBid());
model=new ModelAndView("welcome");
}
else
{
model=new ModelAndView("loginsuccess");
}
}
catch(Exception e)
{
}
return model;
}
}
LoginController.java
enter code here
package com.login.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
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.servlet.ModelAndView;
import com.login.bean.BorewellBean;
import com.login.bean.LoginBean;
import com.login.delegate.BorewellDelegate;
import com.login.delegate.LoginDelegate;
@Controller
@RequestMapping("/loginform")
public class LoginController {
@Autowired
private LoginDelegate loginDelegate;
@RequestMapping(method=RequestMethod.GET)
public ModelAndView showLogin(HttpServletRequest request,HttpServletResponse response)
{
ModelAndView model=new ModelAndView("login");
LoginBean loginbean= new LoginBean();
model.addObject("loginBean",loginbean);
return model;
}
@RequestMapping(method=RequestMethod.POST)
public ModelAndView executeLogin(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("loginBean")LoginBean loginBean)
{
ModelAndView model=null;
try{
boolean isValidUser=loginDelegate.isValidUser(loginBean.getUsername(),loginBean.getPassword());
if(isValidUser)
{
System.out.println("Login successful");
request.setAttribute("loginUser",loginBean.getUsername());
model=new ModelAndView("loginsuccess");
}
else
{
model=new ModelAndView("login");
}
}
catch(Exception e)
{
}
return model;
}
}
SpringBeanConfiguration.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="loginDelegate" class="com.login.delegate.LoginDelegate">
<property name="loginservice" ref="userService"></property>
</bean>
<bean id="userService" class="com.login.service.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean name="userDao" class="com.login.dao.UserDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="borewelldelegate" class="com.login.delegate.BorewellDelegate">
<property name="borewellservice" ref="borewellserviceimpl"> </property>
</bean>
<bean id="borewellserviceimpl" class="com.login.service.Borewellimpl" >
<property name="borewelldao" ref="borewelldao"></property>
</bean>
<bean name="borewelldao" class="com.login.dao.Borewelldaoimpl" >
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/login" />
<property name="username" value="root" />
<property name="password" value="rat" />
</bean>
</beans>
我的错误HTTP状态500 - Servlet调度程序的Servlet.init()引发了异常 创建名为'borewellserviceimpl'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.login.dao.Borewelldao com.login.service.Borewellimpl.borewelldao;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[com.login.dao.Borewelldao]的匹配bean用于依赖:预期至少有1个bean符合此依赖关系的autowire候选资格。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)} Login.jsp页面 在这里输入代码
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form:form action="loginform.html" modelAttribute="loginBean" >
<table>
<tr>
<td>User Name:<FONT color="red"><form:errors path="username" /></FONT> </td>
</tr>
<tr>
<td><form:input path="username" /></td>
</tr>
<tr>
<td>Password:<FONT color="red"><form:errors path="password" /></FONT> </td>
</tr>
<tr>
<td><form:password path="password" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
</body>
</html>
答案 0 :(得分:0)
在服务实现的类中使用@Service,在Dao层中必须在所有DAO类中使用@Repository