0

I created a simple Spring MVC 3 application and want to use freemarker template engine. I cofigure *-context.xml as describes in off Spring's docs, but in browser I get 404 Page not found error. this is my code: 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";
}

}

this is my hello.ftl in WEB-INF/freemarker folder

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

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

and my 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>

what is wrong and why I get 404 when I go to localhost:8080/simple/hello/username ? please, help

EDIT:

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>
Jack Daniel
  • 2,397
  • 8
  • 33
  • 58

1 Answers1

1

its probably wrong configuration for spring mvc, not freemarker. Seems that spring cant find your Controller. Did you add dispatcherServlet in your web.xml?

hi_my_name_is
  • 4,894
  • 3
  • 34
  • 50
  • try to add to your spring xml configuration. – hi_my_name_is Feb 13 '14 at 10:49
  • then I dont see a problem from posted code, please check your startup logs - do they contain info about mapping path to your controller? If there is still no clue - add log4j.logger.org.springframework=DEBUG to log4j.properties – hi_my_name_is Feb 13 '14 at 11:06
  • in this article I find the solution: http://stackoverflow.com/questions/6210757/java-lang-classnotfoundexception-org-springframework-web-context-contextloaderl – Jack Daniel Feb 13 '14 at 11:14