Java SpringMVC控制器bean配置
我正在使用SpringMVC进行一个项目,我解决了一个问题,即启动服务器时自动生成处理程序 代码如下: 控制器Java SpringMVC控制器bean配置,java,spring,spring-mvc,Java,Spring,Spring Mvc,我正在使用SpringMVC进行一个项目,我解决了一个问题,即启动服务器时自动生成处理程序 代码如下: 控制器 @Controller @RequestMapping(value="userstory/{projectid}/{sprintid}/") @SessionAttributes(value = "user") public class UserstoryController { private ISprintService sprintService; private
@Controller
@RequestMapping(value="userstory/{projectid}/{sprintid}/")
@SessionAttributes(value = "user")
public class UserstoryController {
private ISprintService sprintService;
private IUserStoryService userStoryService;
private IProjectService projectService;
private IBurnDownChartService burnDownChartService;
private ITaskService taskService;
public void setSprintService(ISprintService sprintService) {
this.sprintService = sprintService;
}
public void setUserStoryService(IUserStoryService userStoryService) {
this.userStoryService = userStoryService;
}
public void setProjectService(IProjectService projectService) {
this.projectService = projectService;
}
public void setBurnDownChartService(IBurnDownChartService burnDownChartService) {
this.burnDownChartService = burnDownChartService;
}
public void setTaskService(ITaskService taskService) {
this.taskService = taskService;
}
@RequestMapping(method=RequestMethod.GET)
public String getUserstoryPage(@PathVariable("projectid") int pid, @PathVariable("sprintid") int sid, @ModelAttribute("user") SerializablePerson user) {
if (user.getId() != 0) {
Project p = this.projectService.findProjectById(pid);
if (p == null) {
throw new ResourceNotFoundException(pid);
}
if (user.getRole().equals("User")) {
return "userstory/user_userstory";
}
else {
return "userstory/admin_userstory";
}
}
return "redirect:../../../login";
}
}
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="personServiceImpl" class="ch.bsgroup.scrumit.service.impl.PersonServiceImpl" />
<bean id="projectServiceImpl" class="ch.bsgroup.scrumit.service.impl.ProjectServiceImpl" />
<bean id="sprintServiceImpl" class="ch.bsgroup.scrumit.service.impl.SprintServiceImpl" />
<bean id="userStoryServiceImpl" class="ch.bsgroup.scrumit.service.impl.UserStoryServiceImpl" />
<bean id="taskServiceImpl" class="ch.bsgroup.scrumit.service.impl.TaskServiceImpl" />
<bean id="projectService" class="ch.bsgroup.scrumit.service.impl.ProjectServiceImpl" />
<bean id="personService" class="ch.bsgroup.scrumit.service.impl.PersonServiceImpl" />
<bean id="sprintService" class="ch.bsgroup.scrumit.service.impl.SprintServiceImpl" />
<bean id="userStoryService" class="ch.bsgroup.scrumit.service.impl.UserStoryServiceImpl" />
<bean id="burnDownChartService" class="ch.bsgroup.scrumit.service.impl.BurnDownChartServiceImpl" />
<bean id="taskService" class="ch.bsgroup.scrumit.service.impl.TaskServiceImpl" />
<bean id="sprintUserstoryController" class="ch.bsgroup.scrumit.controller.SprintUserstoryController">
<property name="sprintService">
<ref local="sprintService"/>
</property>
<property name="userStoryService">
<ref local="userStoryService"/>
</property>
<property name="projectService">
<ref local="projectService"/>
</property>
<property name="burnDownChartService">
<ref local="burnDownChartService"/>
</property>
<property name="taskService">
<ref local="taskService"/>
</property>
</bean>
<bean id="boardController" class="ch.bsgroup.scrumit.controller.BoardController">
<property name="sprintService">
<ref local="sprintService"/>
</property>
<property name="projectService">
<ref local="projectService"/>
</property>
<property name="userStoryService">
<ref local="userStoryService"/>
</property>
<property name="taskService">
<ref local="taskService"/>
</property>
<property name="burnDownChartService">
<ref local="burnDownChartService"/>
</property>
</bean>
<bean name="register.jsp" id="registerController" class="ch.bsgroup.scrumit.controller.RegisterController">
<property name="personService">
<ref local="personService"/>
</property>
</bean>
<bean id="loginController" class="ch.bsgroup.scrumit.controller.LoginController" >
<property name="personService">
<ref local="personService"/>
</property>
</bean>
<bean id="adminProjectController" class="ch.bsgroup.scrumit.controller.AdminProjectController">
<property name="projectService">
<ref local="projectService"/>
</property>
<property name="personService">
<ref local="personService"/>
</property>
</bean>
<bean id="userProjectController" class="ch.bsgroup.scrumit.controller.UserProjectController">
<property name="projectService">
<ref local="projectService"/>
</property>
</bean>
<bean id="adminPersonController" class="ch.bsgroup.scrumit.controller.AdminPersonController">
<property name="projectService">
<ref local="projectService"/>
</property>
<property name="personService">
<ref local="personService"/>
</property>
</bean>
<bean id="manageProjectMemberController" class="ch.bsgroup.scrumit.controller.ManageProjectMemberController">
<property name="projectService">
<ref local="projectService"/>
</property>
<property name="personService">
<ref local="personService"/>
</property>
</bean>
<bean id="manageUserstoryController" class="ch.bsgroup.scrumit.controller.UserstoryController">
<property name="sprintService">
<ref local="sprintService"/>
</property>
<property name="userStoryService">
<ref local="userStoryService"/>
</property>
<property name="projectService">
<ref local="projectService"/>
</property>
<property name="burnDownChartService">
<ref local="burnDownChartService"/>
</property>
<property name="taskService">
<ref local="taskService"/>
</property>
</bean>
</beans>
错误是:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0': Initialization of bean failed; nested exception is java.lang.IllegalStateException: Cannot map handler 'manageUserstoryController' to URL path [/userstory/{projectid}/{sprintid}/]: There is already handler of type [class ch.bsgroup.scrumit.controller.UserstoryController] mapped.
由于UserstoryController的存在,程序不允许我将UserstoryController映射到manageUserstoryController
有人能给我解释一下为什么会生成userstoryController,以及如何正确地映射它吗。
提前感谢。问题如下。这个元素
<context:component-scan base-package="ch.bsgroup.scrumit.controller" />
可替换为字段上相应的@Autowired
注释。你甚至不需要接球手和接球手。比如说
@Controller
@RequestMapping(value="userstory/{projectid}/{sprintid}/")
@SessionAttributes(value = "user")
public class UserstoryController {
@Autowired
private ISprintService sprintService;
@Autowired
private IUserStoryService userStoryService;
@Autowired
private IProjectService projectService;
@Autowired
private IBurnDownChartService burnDownChartService;
@Autowired
private ITaskService taskService;
@RequestMapping(method=RequestMethod.GET)
public String getUserstoryPage(@PathVariable("projectid") int pid, @PathVariable("sprintid") int sid, @ModelAttribute("user") SerializablePerson user) {
if (user.getId() != 0) {
Project p = this.projectService.findProjectById(pid);
if (p == null) {
throw new ResourceNotFoundException(pid);
}
if (user.getRole().equals("User")) {
return "userstory/user_userstory";
}
else {
return "userstory/admin_userstory";
}
}
return "redirect:../../../login";
}
}
以下是一些相关文档:
app-config.xml
与你的beans.xml
相同吗?嗨,app-config.xml在不同的文件中。我已经用app-config.xml内容更新了帖子。如果我能提供你所需要的所有信息那就太好了。嗨,非常感谢你的回答。我已经解决了这个问题。我只是有一个关于自动连线的小问题:如果我将自动连线添加到其他控制器,我根本不需要beans.xml,对吗?@lightbringer如果您为服务
包添加组件扫描,并用@service
注释所有服务类,那么,不,你不需要它。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0': Initialization of bean failed; nested exception is java.lang.IllegalStateException: Cannot map handler 'manageUserstoryController' to URL path [/userstory/{projectid}/{sprintid}/]: There is already handler of type [class ch.bsgroup.scrumit.controller.UserstoryController] mapped.
<context:component-scan base-package="ch.bsgroup.scrumit.controller" />
<bean id="boardController" class="ch.bsgroup.scrumit.controller.BoardController">
<property name="sprintService">
<ref local="sprintService"/>
</property>
<property name="projectService">
<ref local="projectService"/>
</property>
<property name="userStoryService">
<ref local="userStoryService"/>
</property>
<property name="taskService">
<ref local="taskService"/>
</property>
<property name="burnDownChartService">
<ref local="burnDownChartService"/>
</property>
</bean>
@Controller
@RequestMapping(value="userstory/{projectid}/{sprintid}/")
@SessionAttributes(value = "user")
public class UserstoryController {
@Autowired
private ISprintService sprintService;
@Autowired
private IUserStoryService userStoryService;
@Autowired
private IProjectService projectService;
@Autowired
private IBurnDownChartService burnDownChartService;
@Autowired
private ITaskService taskService;
@RequestMapping(method=RequestMethod.GET)
public String getUserstoryPage(@PathVariable("projectid") int pid, @PathVariable("sprintid") int sid, @ModelAttribute("user") SerializablePerson user) {
if (user.getId() != 0) {
Project p = this.projectService.findProjectById(pid);
if (p == null) {
throw new ResourceNotFoundException(pid);
}
if (user.getRole().equals("User")) {
return "userstory/user_userstory";
}
else {
return "userstory/admin_userstory";
}
}
return "redirect:../../../login";
}
}