不能在Spring MVC 3中使用freemarker

时间:2014-02-13 09:55:33

标签: spring spring-mvc freemarker

我创建了一个简单的Spring MVC 3应用程序,并希望使用freemarker模板引擎。我按照Spring的文档描述了* -context.xml,但在浏览器中我得到了404 Page not found错误。这是我的代码: 的 HelloWorldController.java

@Controller
@RequestMapping("/hello")
public class HelloWorldController {

private static final Logger log = Logger.getLogger(HelloWorldController.class);

@RequestMapping(value="/{name}", method = RequestMethod.GET)
public String hello(@PathVariable String name, Model model) {

    String result = "Hello, " + name;
    model.addAttribute("result", result);

    return "hello";
}

}

这是我在WEB-INF / freemarker文件夹中的 hello.ftl

<!doctype html>
<html>
<head>
    <title>Hello</title>
</head>
<body>
<h1>
    Hello world!  
</h1>

<P>  The time on the server is ${result}. </P>
</body>
</html>

和我的 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"
    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">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- freemarker config -->
    <beans:bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <beans:property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
    </beans:bean>

    <!-- 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.freemarker.FreeMarkerViewResolver">
        <beans:property name="prefix" value="" />
        <beans:property name="suffix" value=".ftl" />
        <beans:property name="cache" value="false" />
    </beans:bean>

    <context:component-scan base-package="org.example.simple" />

</beans:beans>

有什么问题,为什么我去localhost时得到404:8080 / simple / hello / username? 请帮忙

修改

的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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

1 个答案:

答案 0 :(得分:1)

它可能是错误的配置为spring mvc,而不是freemarker。似乎春天找不到您的控制器。您是否在web.xml中添加了dispatcherServlet?