Spring MVC - 无法呈现视图

时间:2014-11-26 17:09:25

标签: java spring-mvc model-view-controller views

我正在学习spring-mvc,我使用STS创建了一个crud项目,它工作正常。

我需要创建另一个视图,仅用于测试和使用spring:

  1. 我在与其他jsp文件(WEB-INF / views)相同的位置添加了一个新的jsp文件,名为company.jsp
  2. 我创建了一个新的控制器来处理请求:
  3. package com.test.issa;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    @Controller
    public class CompanyController {
    
      @RequestMapping(value = "/company", method = RequestMethod.GET)
        public String GetCompany(Model model) {
          model.addAttribute("name", "Hello World!");
            return "company";
        }
    }
    

    但是当我调用视图http:// localhost:9999 / app / company时它会给出错误404,我错过了什么吗?

    这也是我的servlet-context.xml:

    <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    
        <!-- DispatcherServlet Context: defines this servlet's request-processing 
            infrastructure -->
    
        <!-- Enables the Spring MVC @Controller programming model -->
        <annotation-driven />
    
        <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
            up static resources in the ${webappRoot}/resources directory -->
        <resources mapping="/resources/**" location="/resources/" />
    
        <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
            in the /WEB-INF/views directory -->
        <beans:bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <beans:property name="prefix" value="/WEB-INF/views/" />
            <beans:property name="suffix" value=".jsp" />
        </beans:bean>
    
        <!-- Hibernate 4 SessionFactory Bean definition -->
        <beans:bean id="hibernate4AnnotatedSessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <beans:property name="dataSource" ref="dataSource" />
            <beans:property name="annotatedClasses">
                <beans:list>
                    <beans:value>com.journaldev.spring.model.Person</beans:value>
                </beans:list>
            </beans:property>
            <beans:property name="hibernateProperties">
                <beans:props>
                    <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
                    </beans:prop>
                    <beans:prop key="hibernate.show_sql">true</beans:prop>
                </beans:props>
            </beans:property>
        </beans:bean>
    
        <beans:bean id="personDAO" class="com.journaldev.spring.dao.PersonDAOImpl">
            <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
        </beans:bean>
        <beans:bean id="personService" class="com.journaldev.spring.service.PersonServiceImpl">
            <beans:property name="personDAO" ref="personDAO"></beans:property>
        </beans:bean>
        <context:component-scan base-package="com.journaldev.spring" />
    
        <tx:annotation-driven transaction-manager="transactionManager"/>
    
        <beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
        </beans:bean>    </beans:beans>
    

1 个答案:

答案 0 :(得分:1)

您只是扫描此处声明的包的组件

<context:component-scan base-package="com.journaldev.spring" />

但您的@Controller已在

中定义
package com.test.issa;

您需要将该包添加到component-scan,否则将不会为@Controller注释类型创建一个bean,并且MVC堆栈将不会注册其处理程序方法。

或者为<bean>类型声明明确的CompanyController