Spring MVC - 映射到.htm并不起作用

时间:2015-12-22 20:12:46

标签: spring-mvc servlets

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

拜托,你能帮我解决问题吗?

这是Controller类

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" %>

这是我在这里的第一个问题,非常敬佩你们所有人,抱歉我的英语很差!

1 个答案:

答案 0 :(得分:-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);

}

我希望这对你有用..