1

I know there were a thousand questions like this, but I m blind and can not find error. For this setup for .jsp page everything works fine (after pointing http://localhost:8082/spring/ Suprisingly, after http://localhost:8082/spring/WEB-INF/spring/static/index.jsp page is not loading). I'm new in this, so probably I'm missunderstanding what I'm doing/

When I switch to <property name="suffix" value=".html" /> (I have 2 files index.jsp and index.html) I can not load page neither from http://localhost:8082/spring/ or this http://localhost:8082/spring/WEB-INF/spring/static/index.html link.

mar 14, 2015 8:38:07 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/spring/WEB-INF/spring/static/index.html] in DispatcherServlet with name 'appServlet'

servlet-context.xml:

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

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/spring/static/" />
        <property name="suffix" value=".html" />
    </bean>

    <context:component-scan base-package="net.codejava.spring" />

    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/usersdb"/>
        <property name="username" value="root"/>
        <property name="password" value="1234"/>
    </bean> 

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
    </bean>

    <tx:annotation-driven />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="userDao" class="net.codejava.spring.dao.UserDAOImpl">
        <constructor-arg>
            <ref bean="sessionFactory" />
        </constructor-arg>
    </bean>     
</beans>

HomeController:

@Controller
public class HomeController {

    @Autowired
    private UserDAO userDao;

    @RequestMapping(value="/")
    public ModelAndView home() {
        List<User> listUsers = userDao.list();
        //ModelAndView model = new ModelAndView("home");
        ModelAndView model = new ModelAndView("index");
        model.addObject("userList", listUsers);
        return model;
    }

}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.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>
Piotr M
  • 499
  • 3
  • 9
  • 27
  • do you have `` in your servlet-context.xml? – minion Mar 14 '15 at 19:57
  • Yes I have. Now I updated servlet-context.xml, I could not post before, idk why. Thanks for intrest. – Piotr M Mar 14 '15 at 20:00
  • What is your web context and what is the url you are hitting? – minion Mar 14 '15 at 20:06
  • so you request `http://localhost:8080/spring/static/index.html` and expect `HomeController.home()` to be invoked? – fateddy Mar 14 '15 at 20:13
  • Url : http://localhost:8082/spring/ . I don't understand question about web context. Also, I tried to change @RequestMapping(value="/") for value="/spring". Did not work. – Piotr M Mar 14 '15 at 20:16
  • the warning log tells me that you request `http://....:8082/spring/static/index.html` - or do you really (please double check that) get that warning log if you invoke `http://.....:8082/spring`? – fateddy Mar 14 '15 at 20:24
  • At both `8082/spring` and `8082/spring/static/index.html` I got `HTTP Status 404 -`. – Piotr M Mar 14 '15 at 20:30
  • Just spotted another thing - .jsp files not work as well. `message /spring/static/home.jsp description The requested resource is not available.` Got this error after changing suffix to .jsp (I added home.jsp into described folder). So how is it possible that tomcat does not see my files. When I was requesting files into WEB-INF it was working fine (.jsp files, not html). – Piotr M Mar 14 '15 at 20:41
  • Can you try http://localhost:8082/ and tell me what is going on? – minion Mar 14 '15 at 20:57
  • It is pointed for welcome page of tomcat. I had edited my 1 post, maybe it is more clear now. – Piotr M Mar 14 '15 at 21:13

2 Answers2

2

There are a few things to understand to realize where the problem is

Firstly, you cannot access directly the pages under WEB-INF that is why your JSPs are not rendered when you directly attempt the http://localhost:8082/spring/WEB-INF/spring/static/index.jsp

For what concerns the html files, the problem is that when you hit the contoller a view name is constructed and used in the request forward. Your view name will be /WEB-INF/spring/static/index.html and it will search for a servlet that should process the request with this extension and it will fail (if it were to be .jsp it would be process by JSPServlet). As no servlet will be able to handle the request, a default servlet will kick-in and try to find a controller method to handle it and will fail with the message no mapping found for /WEB-INF/spring/static/index.html

Your best bet is to configure the view results of your controllers as JSP. Otherwise, there's the configuration that seem to worked for the following answer or try to register html files to be handled by the JSP servlet, as explained here

Community
  • 1
  • 1
Master Slave
  • 27,771
  • 4
  • 57
  • 55
1

First of all - your web.xml is more than you just put in your file. For example in Tomcat, your web.xml file gets merged with Tomcat default web.xml (see conf/web.xml in Tomcat's dir). There you can find, that URLs in form of *.jsp are mapped to JSP Servlet.

The Java Web Apps work in terms of servlets (you map them to URLs in web.xml). For example you mapped DispatchServlet to an URL of /. This dispatch servlet "magically" works with Spring and loads your controllers - and process their mappings "inside". Thats why you used @RequestMapping(value="/").

Inside the controller you basically commands to dispatch your request to a view with name index - which then gets resolved by InternalResourceViewResolver to form a path of /WEB-INF/spring/static/ + index + .jsp - just like you configured in context's descriptor file.

This, basically, issues an internal dispatch request (not exactly, but you can think of it this way for now) - which then gets handled using exactly the same rules at the first request - for example being caught by *.jsp servlet and then processed. However, in no way the request like /WEB-INF/spring/static/index.html can be processed - no rules matching can be found.

To answer your question - you can put any HTML in the JSP files - using any JSP tags its not mandatory. If you, however, want to really use plain HTML files without any parsing - then think of using static resources - you already set it up in <mvc:resources mapping="/resources/**" location="/resources/" /> - this basically exposes content of your /resources/ directory to the world without any processing.

M4ks
  • 11,744
  • 7
  • 27
  • 48