error 404 when trying to access a route created in spring

0

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/app/*</url-pattern><!--*.htm-->
</servlet-mapping>

<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>

dispatcher-servlet.xml

<?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:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="Controller"/>
<context:component-scan base-package="RestController"/>
<context:component-scan base-package="ServiceImpl"/>


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

</beans>

PrincipalContoller.java

import Entidades.Usuario;
import ServiceImpl.UsuarioServiceImpl;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class PrincipalController {
@Autowired
UsuarioServiceImpl usuarioService;


@RequestMapping(value="/app/index.htm", method= RequestMethod.GET)
public ModelAndView listar(){
    List datos=usuarioService.listar();
    ModelAndView mv=new ModelAndView();
    mv.setViewName("index");
    mv.addObject("usuarios",datos);
    mv.addObject("user",new Usuario());
    System.out.println("OBJ: "+datos);
    return mv;
}

@RequestMapping(value="/app/agregar.htm", method= RequestMethod.POST)
public ModelAndView agregar(Usuario u){
    usuarioService.agregar(u);
    return new ModelAndView("redirect:/app/index.htm");
}

@RequestMapping(value="/app/editar.htm", method= RequestMethod.POST)
public ModelAndView editar(HttpServletRequest request){
    int id=Integer.parseInt(request.getParameter("id"));
    String contrasena=request.getParameter("contrasena");
    usuarioService.cambiarContrasena(contrasena, id);
    return new ModelAndView("redirect:/app/index.htm");
}
}

Error: When I try to enter: link I get this warning:

    Información:   Mapped URL path [/app/editar.htm] onto handler 'principalController'
    Información:   Mapped URL path [/app/index.htm] onto handler 'principalController'
    Información:   Mapped URL path [/app/agregar.htm] onto handler 'principalController'
    Información:   FrameworkServlet 'dispatcher': initialization completed in 4156 ms
    Información:   Loading application [springMVC] at [/springMVC]
    Información:   Loading application springMVC done in 12.965 ms
    Advertencia:   No mapping found for HTTP request with URI [/springMVC/app/index.htm] in DispatcherServlet with name 'dispatcher'

And in the browser: HTTP Status 404 - Not Found

But if I change the servlet mapping of the DispatcherServlet to "* .htm" I can access normally. But I need to just do the mapping to the routes that are inside "/ app /", besides that forces me to put my routes a termination ".htm"

    
asked by Omar Villanueva 03.01.2019 в 18:26
source

1 answer

1

Because you're repeating / app in web.xml

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/app/*</url-pattern>
</servlet-mapping>

as in PrincipalController

@RequestMapping(value="/app/index.htm", method= RequestMethod.GET)
public ModelAndView listar(){
    List datos=usuarioService.listar();
    ModelAndView mv=new ModelAndView();
    mv.setViewName("index");
    mv.addObject("usuarios",datos);
    mv.addObject("user",new Usuario());
    System.out.println("OBJ: "+datos);
    return mv;
}

you just need to modify the value of the @RequestMapping instead of:

@RequestMapping (value=" /app/index.htm ", method = RequestMethod.GET)

to make them look like this:

@RequestMapping (value=" /index.htm ", method = RequestMethod.GET)

to call it like this:

 http://localhost:8080/tuContexto/app/index.htm

and not like this:

 http://localhost:8080/tuContexto/app/app/index.htm

and the web.xml , does not suffer modification.

    
answered by 04.01.2019 / 23:36
source