Imesh Chandrasiri
Imesh Chandrasiri

Reputation: 5679

Spring controller not showing the relevent view

I've got the following spring controller class.

@Controller
public class LayoutController {

    @RequestMapping(value = "/layout", method = RequestMethod.GET)
    public String ShowLayout(){
        return "redirect:/views/layout.html";
    }
}

This is my common-config xml.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

        <!--  COMMON CONFIGURATIONS -->
        <mvc:annotation-driven/>
        <tx:annotation-driven/> 

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

        <!-- Annotations based Configuration -->
        <context:annotation-config />

        <mvc:resources mapping="/*" location="/WEB-INF/views/" />

        <!-- Components Auto-Detection (Backend) -->
        <context:component-scan base-package="com.jkcs.touchpos" use-default-filters="false" >
            <!-- Types annotated by Spring Managed, Controller and Transactional, or by an annotation that itself is 
            annotated by SpringManaged, Controller, Transactional -->
            <context:include-filter type="annotation" expression="com.jkcs.touchpos.platform.annotations.SpringManaged"/>
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
            <context:include-filter type="annotation" expression="org.springframework.transaction.annotation.Transactional"/>
        </context:component-scan>

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

</beans>

This is my mvc-config xml file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
</beans>

This is my web.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 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" id="WebApp_ID" version="2.5">
  <display-name>TouchPOS</display-name>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

finally the dispatcher servlet :

<?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:context="http://www.springframework.org/schema/context"
    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">

    <!--  SPECIFIC CONFIGURATIONS -->
    <import resource="springConfigurations/common-config.xml"/>
    <import resource="springConfigurations/mvc-config.xml"/>

</beans>

When I run this web app on a jetty server, and I use the following URL to get the layout. But the server throws a 404 error. please help me to identify the wrong thing I'm doing?

http://localhost:8080/TouchPOSApplicatio/layout

Update : The web page elements show properly but with no CSS. And the console printed the following error.

Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/style/style.css] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/style/jquery.custom-scrollbar.css] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/js/jquery-1.9.1.min.js] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/js/jquery.colorbox.js] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/js/jquery.touchSwipe.min.js] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/js/functions.js] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/js/dragscrollable.js] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/js/jquery-ui.js] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/style/colorbox.css] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/js/jquery.colorbox.js] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/js/functions.js] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/js/jquery.touchSwipe.min.js] in DispatcherServlet with name 'dispatcher'
Apr 19, 2013 2:17:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TouchPOSApplication/js/dragscrollable.js] in DispatcherServlet with name 'dispatcher'

Upvotes: 0

Views: 1048

Answers (2)

NilsH
NilsH

Reputation: 13821

I assume TouchPOSApplicatio is just a typo in the question? I'm strugglig to understand your pattern here. You have mapped static resources to /WEB-INF/views, which is not accesible from the browser, so I don't think that'll work. Try putting it into a regular webapp folder instead, like static (at the same level as WEB-INF). Then change your config to:

<mvc:resources mapping="/static/**" location="/static"/>

and then you can verify that it can be accessed by typing http://localhost:8080/TouchPOSApplication/static/layout.html. If that works, then you have configured the static resources correctly.

Edit

If you have css resources also, make sure to add the folder with those resources also as a mvc:resource.

Upvotes: 1

WilQu
WilQu

Reputation: 7393

Your controller is mapped to @RequestMapping(value = "/layout", method = RequestMethod.GET), and your servlet mapping is <url-pattern>/</url-pattern>, so the URL should be http://localhost:8080/layout.

Besides, as NilsH already said, your controller redirect to a file that the browser can not see directly. It should be exposed as a static file.

Upvotes: 0

Related Questions