I can’t access a form page

Asked

Viewed 140 times

3

I am doing a course of Alura of Spring framework, but I can not access a form page through the link http://localhost:8080/casadocodigo/products/form. Gives the following error:

HTTP Status 404 - Not Found Type Status Report

Message /casadocodigo/products/WEB-INF/views/products/form.jsp

Description The origin server Did not find a Current representation for the target Resource or is not willing to disclose that one exists.

Apache Tomcat/9.0.5

I believe that is the way that is wrong. I think that should not have this /produtos between the name of the project and /WEB-INF. Is that the problem? Why is he adding this /produtos?

Follow the controller source and configuration class below:

Class Productoscontroller:

package br.com.casadocodigo.loja.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import br.com.casadocodigo.loja.daos.ProdutoDAO;
import br.com.casadocodigo.loja.models.Produto;

@Controller
public class ProdutosController {

    @Autowired
    private ProdutoDAO produtoDao;

    @RequestMapping("/produtos")
    public String gravar(Produto produto) {
        System.out.println(produto);
        this.produtoDao.gravar(produto);
        return "produtos/ok";
    }

    @RequestMapping("/produtos/form")
    public String form() {
        System.out.println("Entrando em método FORM");
        return "produtos/form";
    }

}

Appwebconfiguration class:

package br.com.casadocodigo.loja.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import br.com.casadocodigo.loja.controllers.HomeController;
import br.com.casadocodigo.loja.daos.ProdutoDAO;

@EnableWebMvc
@ComponentScan(basePackageClasses={HomeController.class, ProdutoDAO.class})
public class AppWebConfiguration {

    @Bean
    public InternalResourceViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}
  • Your jsp is inside a folder called produtos?

  • Yes. /WEB-INF/views/products/form.jsp I ran a test and changed @Requestmapping("/products/form") by @Requestmapping("/form") and was able to access the form. Apparently it is giving conflict in the mapping of similar url’s. Can you tell me if this is to happen or is it normal? 'Cause I followed it exactly like the instructor did.

  • I understand what happened. If @Requestmapping I put two words separated by "/" it adds the previous one between the project name and the /WEB-INF. Was this supposed to happen? And if I want to set up a url with more than one word as it was done in the course I’m doing?

  • 1

    It doesn’t make much sense. Spring is prepared to handle multiple "/" urls. Try placing a "/" at the beginning of the string on the line resolver.setPrefix("WEB-INF/views/");

  • Putting the "/" at the beginning of the prefix worked. Thank you so much for the help!

  • @Statelessdev, how would you feel about adding an answer since you solved the Rubens problem? :)

  • @Dherik passed me beaten his comment that there solved the problem. Thank you for alerting me. Answer created.

Show 2 more comments

1 answer

1

Spring is prepared to handle multiple urls "/". Modify this line:

resolver.setPrefix("WEB-INF/views/");

for

resolver.setPrefix("/WEB-INF/views/");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.