【问题标题】:Why won't my spring mvc views load?为什么我的 spring mvc 视图不会加载?
【发布时间】:2015-08-02 19:28:23
【问题描述】:

我是 spring mvc 的新手。我目前正在使用 netbeans 8.0.2。以及一个spring mvc插件。从我在项目的“库”部分下看到的 jar 文件中,我似乎使用的是 spring mvc 4.0.1。

我可以从 tomcat 服务器甚至 glassfish 加载 index.jsp 页面,但是如果我应该在目录结构 WEB-INFO>jsp>response.jsp 下创建另一个 .jsp 文件一个例子。我从服务器收到错误 404。这可能是一个非常简单的修复方法,但到目前为止还没有成功。

我怀疑这与我的控制器和 [springapp]-servlet.xml 文件有关。我在网上看到的大多数示例都使用 spring mvc 3.x 并避免在控制器设置中使用注释方法,乍一看后者对我来说似乎更容易。然后这些示例继续向上述 xml 文件添加几行。但是,我认为通过使用注释控制器设置,spring mvc 会自动检测我的控制器。不推荐使用注解方式吗?

我的配置文件如下:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>springapp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>

    </servlet>
    <servlet-mapping>
        <servlet-name>springapp</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

springapp-servlet.xml:

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
                            http://www.springframework.org/schema/mvc 
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                            http://www.springframework.org/schema/context 
                            http://www.springframework.org/schema/context/spring-context-40.xsd
                            http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">


    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>


    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>


    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />



</beans>

其他相关文件:

控制器:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
//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.bind.annotation.RequestParam;

/**
 *
 * @author macj7
 */

@Controller
public class HelloController {


    @RequestMapping( value = "/response", method = RequestMethod.GET)
    public String index( Model model) {
        String name = "World";
        model.addAttribute("name", name );
        return "response";
    }

}

response.jsp:

<%-- 
    Document   : response
    Created on : Jul 31, 2015, 1:25:19 PM
    Author     : macj7
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Spring Web MVC </title>
    </head>
    <body>
        <h1> th:text= "Hello, ${name}!" </h1>
    </body>
</html>

redirect.jsp:

<%--
Views should be stored under the WEB-INF folder so that
they are not accessible except through controller process.

This JSP is here to provide a redirect to the dispatcher
servlet but should be the only JSP outside of WEB-INF.
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% response.sendRedirect("index.htm"); %>

如果有此问题的解决方案或更多信息 需要请告诉我。我也想解释任何答案 在我尝试学习时给出,而不仅仅是复制和粘贴解决方案。 这是我第一次尝试使用 java 创建 Web 应用程序,我必须说仅配置似乎相当令人生畏。

提前感谢您的时间和耐心。

【问题讨论】:

  • 我怀疑您不知道如何正确打包和部署 Web 应用程序。
  • 不,目前没有。这就是为什么我决定使用像 netbeans 这样的 IDE 来帮助我。如果您有任何建议,请随时分享。
  • 在您知道自己在做什么之前,您不应该使用 IDE。现在你有两个问题。
  • 我通常会同意,但我只是想尽快测试框架,并认为鉴于我的经验或缺乏 netbeans 将处理它所具有的大量基本设置。无论如何感谢您的建议。
  • 你需要一个 JSP 视图解析器:stackoverflow.com/questions/9970035/…

标签: java jsp spring-mvc netbeans-8


【解决方案1】:

我认为在 web.xml 文件的欢迎文件列表中,您应该提供 Spring Controller 请求映射而不是 jsp 文件名,如下所示。

<welcome-file-list>
    <welcome-file>/response</welcome-file>
</welcome-file-list>

【讨论】:

  • 没想到一个会起作用,结果没有。感谢您的意见。
【解决方案2】:

好的,所以我找到的解决方案是将在 springapp-servlet.xml 中处理请求的控制器定义为 bean,并在控制器本身中重新定义该方法,以便它返回一个 ModelAndView (import org. springframework.web.servlet.ModelAndView;)。 我的页面现在呈现。

我应该注意到我已经重写了模型,但没有使用注释形式来编写控制器,但我很快也会以这种形式对其进行测试。现在我只是简单地实现了控制器接口。

感谢所有回复的人。感谢您的意见。

【讨论】:

    【解决方案3】:

    根本原因:

    1. 根据web.xml DispatcherServlet 查找以*.htm 结尾的url,它不接受response.jsp 的请求

    注意:

    在启动时加载 DispatcherServlet 作为优先级 1

    <load-on-startup>1</load-on-startup>
    

    解决方案:

    更改将检查每个请求的DispatcherServlet 的映射。

    <servlet-mapping>
        <servlet-name>springapp</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    

    要访问response.jsp,您需要输入根据控制器中定义的RequestMapping 映射的url www.domain/context-root/response

    阅读更多关于Web MVC framework - The DispatcherServlet

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多