【问题标题】:Spring MVC - Mapping to .htm doesn't workSpring MVC - 映射到 .htm 不起作用
【发布时间】:2015-12-22 20:12:46
【问题描述】:

我想按照教程一步一步地创建一个简单的 hello world,但我认为缺少一些东西,因为我的代码只返回 404

请问您能帮我解决问题吗?

这是控制器类

package com.companyname.springapp;
import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/**
 * Created by cota on 22/12/15.
 */
@Controller
public class HelloController{

protected final Log logger = LogFactory.getLog(getClass());

@RequestMapping(value="/hello.htm",method = RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response)
        throws ServletException, IOException {

    String now = (new Date()).toString();
    logger.info("Returning hello view with " + now);

    return new ModelAndView("views/hello.jsp", "now", now);

}
}

这是 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     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">

<display-name>Springapp</display-name>

<servlet>
    <servlet-name>springapp</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>springapp</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

这是 app-config.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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="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/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <!-- Configures the @Controller programming model -->
    <mvc:annotation-driven/>

    <!-- Scans the classpath of this application for @Components to deploy as beans -->
    <context:component-scan base-package="com.companyname.springapp.web" />

</beans>

这是一个包含所有JSP文件的头文件

<%@ page session="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

并且 index.jsp 和 hello.jsp 有下一行

<%@ include file="views/include.jsp" %>

这是我在这里的第一个问题,真的很佩服你们所有人,对不起我的英语不好!

【问题讨论】:

  • ...您的视图解析器配置是什么?我认为您还有其他问题,但最好一步一步来。
  • 你打什么端点?当你的服务器启动时,你会看到什么输出?
  • 问题很可能是因为 Spring 找不到文件 views/hello.jsp。你是怎么配置ViewResolver的?
  • 你可以发布你的网址吗?

标签: spring-mvc servlets


【解决方案1】:

我不认为你没有正确配置视图解析器。所以,请尝试以下链接。它可能对你有用。 打开您的 app-config.xml 并添加以下行。

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

并如下修改您的控制器。

   @RequestMapping(value="/hello.htm",method = RequestMethod.GET)
   public ModelAndView handleRequest(HttpServletRequest     request,HttpServletResponse response)
    throws ServletException, IOException {

String now = (new Date()).toString();
logger.info("Returning hello view with " + now);

return new ModelAndView("hello", "now", now);

}

我希望这对你有用..

【讨论】:

  • 您为什么建议进行这些更改?他们将完成什么?以前怎么错了?
  • ModelAndView("views/hello.jsp", "now", now) 如果他已经配置了 viewResover,他如何返回带有 .jsp 扩展名的视图。他是因为配置。 @Coat 没有提到那些事情。
  • 对。我希望您编辑您的答案并澄清所有这些。
猜你喜欢
  • 2014-05-15
  • 1970-01-01
  • 2017-04-07
  • 2016-01-31
  • 2014-06-12
  • 1970-01-01
  • 2013-10-13
  • 1970-01-01
  • 2017-03-05
相关资源
最近更新 更多