当前位置: 首页 > 知识库问答 >
问题:

Spring mvc控制器bean配置

慕高格
2023-03-14

我正在使用Spring mvc进行一个项目,我解决了一个问题,即启动服务器时自动生成处理程序。

以下是代码:

控制器

@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";
    }

}

豆。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>

我复制所有bean以显示没有用户故事控制器映射。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
    <servlet-name>scrumit</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/app-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>scrumit</servlet-name>
    <url-pattern>/app/*</url-pattern>
  </servlet-mapping>
</web-app>

应用程序配置。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:context="http://www.springframework.org/schema/context"
    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">

    <!-- Scans the classpath of this application for @Components to deploy as beans -->
    <context:component-scan base-package="ch.bsgroup.scrumit.controller" />

    <!-- Application Message Bundle -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/messages/messages" />
        <property name="cacheSeconds" value="0" />
    </bean>

    <!-- Including beans -->
    <import resource="beans.xml" />

    <!-- Configures Spring MVC -->
    <import resource="mvc-config.xml" />

</beans>

这就是我启动服务器时的情况:

INFO : org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped URL path [/userstory/{projectid}/{sprintid}/] onto handler 'userstoryController'

错误是:

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映射到Management eUserStoryController,因为userStoryController的存在。

谁能给我解释一下为什么会生成用户故事控制器,以及如何正确地映射它。谢谢。

共有1个答案

范承望
2023-03-14

问题如下。这个元素

<context:component-scan base-package="ch.bsgroup.scrumit.controller" />

由于@controller,已经创建了UserstoryController控制器的实例。然后是

当您用@Controller注释一个类并具有适当的组件扫描时,您不应该在上下文中声明单独的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>

可以在字段上用适当的@Autoween注释替换。您甚至不需要getter和setters。例如

@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";
    }
}

以下是一些相关的留档:

  • @Component和@Repository之间有什么区别

 类似资料:
  • 我一直在尝试使用: 使用此链接: 但我有一个错误: 当我换成: 是工作。我能做些什么来和日期一起工作? 谢啦

  • 我有过 我通过这种方式传递profileJson: 但是我的配置文件Json对象具有所有空字段。我应该怎么做才能让Spring解析我的json?

  • 我正在使用Spring形式。我只需要得到Staemap作为响应,但我得到的是整个jsp页面作为响应。

  • > 我有两个控制器(ControllerA和ControllerB) 两个控制器都调用一个服务(MyService)。 MyService调用名为MyRepository的接口,该接口有两个实现(FirstRepository和SecondRepository)。 如何可能在从ControllerA调用服务(MyService)时使用FirstRepository而在调用来自ControllerB

  • 21.4. 控制bean的 ObjectName 在后台,MBeanExporter 委派 ObjectNamingStrategy 的一个实现去获取正在注册的每个bean的ObjectName。 缺省的实现是 KeyNamingStrategy,它缺省用 beans Map 的键作为 ObjectName。 此外,KeyNamingStrategy 能把beans Map 的键映射为一个 Pro

  • 我想使用@SessionAttributes注释在SpringMVC中的两个控制器之间共享会话属性。 下面是我用来测试属性共享的一个简单代码:AController。JAVA a.jsp BController.java b.jsp 我期望的行为是转到 /aURL,myParam将被设置为0到99之间的随机值,然后该值将在两个控制器之间共享。 但是,会发生以下情况:我转到/a URL,myPara