3
votes

Yes. I know this is familiar issue. I have looked into other solutions and they didn't help me. I am trying to build Spring MVC Application with Spring 4, Hibernate 5, My Sql and Angular JS 1.x

Problem: As shown in image, When I run the application, it resolved to index.jsp file as expected, then I entered 'http://localhost:8080/TimeLee/user/test' to get web page 'adduser.html' . Boom, it throws following error 'No mapping found for HTTP request with URI [/TimeLee/WEB-INF/views/adduser.html] in DispatcherServlet with name 'mvc-dispatcher'. I checked controller mappings and everything looks good.

Folder Structure and Error message from log

Browser Output

UserController:

package com.timelee.users.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.timelee.users.model.User;
import com.timelee.users.services.UserService;

@RestController
@RequestMapping("/user")
public class UserController
{
    @Autowired
    private UserService userService;

    @RequestMapping(value="/adduser",method=RequestMethod.POST)
    public void saveUser(@RequestBody User user)
    {
        userService.saveUser(user);
    }


    @RequestMapping(value="/getuser",method=RequestMethod.GET)
    public @ResponseBody User getUser(@RequestParam("userId") String userId)
    {
        return userService.getUser(userId);
    }   

    @RequestMapping(value="/test",method=RequestMethod.GET)
    public  ModelAndView test()
    {
        ModelAndView modelAndView=new ModelAndView("adduser");
        return modelAndView;
    }

    @RequestMapping(value="/test2",method=RequestMethod.GET)
    public  String test2()
    {
        return "adduser";
    }

}

web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring Web MVC Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-mvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


</web-app>

spring-mvc-config.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

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


    <context:property-placeholder location="classpath:database.properties" />
      <context:component-scan  base-package="com.timelee.*" />


    <tx:annotation-driven transaction-manager="transactionManager"/>


     <!--  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  -->

     <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
       <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}"/>

    </bean>



    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan">
            <list>
                <value>com.timelee.timesheet.model</value>
                <value>com.timelee.users.model</value>
                <value>com.timelee.*</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql:false}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql:false}</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>       
    </bean>

    <bean id="transactionManager"  class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="persistenceExceptionTranslationPostProcessor"
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

</beans>
4
Just add <mvc:annotation-driven/> and clean your project from Eclipse-->Menubar--->Projrct--->Clean and restart the projectCoder ACJHP
I tried this. It didn't work. I still see the errorJohn
you're working with the views in the bounds of @RestController class, that's wrong, use the @Controller instead. The @RestController annotation is the shortcut for @ResponseBody to be specified at the each controller's method. For the now, there're objects to be returned will be converted to JSON and passed straight into the server response instead of view templates lookup.WildDev
Thanks for the info Will. Would it be possible to use one controller for both (REST calls and User Requests)?John
@John, yes. You have to specify @ResponseBody annotation at the each REST method before the return value declaration inside @Controller annotated сlass.WildDev

4 Answers

4
votes

Add <mvc:default-servlet-handler/> in spring-mvc-config.xmlfile. Clean the project and rebuild it. This may solve your issue.

1
votes

I guess try to add in spring-mvc-config.xml. Hope this will work for you. Other things looks good.

<mvc:annotation-driven />
<mvc:default-servlet-handler/> 

Next try is add those code in the top of web.xml file. it seems different in your case.

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">

 </web-app>

If it does not work for you then follow my link and see the difference web.xml as well as application-context.xml and dispatcher-servlet.xml

0
votes

First of all i want to know that is adduser is a jap or HTML file? If yes than use simple return for it.

Second is you create adduser post method in your controller, right? Add adduser get method in controller.
Like

@requestmapping(value="adduser",requestmethod.GET)
public String adduser(){
     Return // return page name
}
0
votes

First, is adduser is a jap or HTML file? If HTML then use simple return for it.

Second, is your create adduser POST method in your controller? Add adduser GET method in controller.

Like:

@requestmapping(value="adduser",requestmethod.GET)
public String adduser(){
     Return // return page name
}

Here, the 404 error only shows that there is no adduser.html or adduser.jsp available in your webinf folder or where you put all of your jap/HTML files.