java - Spring MVC - 无法渲染 View

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

我正在学习 spring-mvc,我使用 STS 创建了一个 CRUD 项目,并且运行良好。

我需要创建另一个 View 只是为了测试和使用 spring:

  1. 我在与其他 jsp 文件 (WEB-INF/views) 相同的位置添加了一个新的 jsp 文件,名为 company.jsp
  2. 我创建了一个新 Controller 来处理请求:
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";
    }
}

但是当我调用 View 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>

最佳答案

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

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

但是你的@Controller定义于

package com.test.issa;

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

要么声明一个显式 <bean>为您CompanyController类型。

关于java - Spring MVC - 无法渲染 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27155008/

相关文章:

Java SQL 语句 - 索引数组越界

java - 为什么我在 Java 中收到有关 "Possible loss of precision"的警告?

Spring MVC 3 - 将 'immutable' 对象绑定(bind)到表单

java - 基本的 Spring MVC 数据绑定(bind)

java - Spring 默认语言环境未按我对 CookieLocaleResolver 的预期工作

javascript - 无法让简单的 agility.js 工作

php - 什么是 "real"MVC 模式?

java - 在同一方法中修改 ConcurrentHashMap 和 Synchronized ArrayList

java - 如何将 GlideBitmapDrawable 相互转换为 BitmapDrawable

java - Web 应用程序中的 ContextLoaderListener 和 Servlet 上下文是什么?