I cannot view the jsp page of a Spring MVC project

Asked

Viewed 323 times

0

I cannot see the form.jsp page

I am putting the following URL http://localhost:8080/Store/

With this URL it only views the hello-world.jsp page

I even put the following address http://localhost:8080/Shop/products/form but it creates error, wonder where I’m going wrong.

here is my complete project;

https://github.com/wladimirbandeira/Loja/tree/master/Loja

part1;

public class ServletSpringMVC extends AbstractAnnotationConfigDispatcherServletInitializer{

    @Override
    protected Class<?>[] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return null;
    }


    // esse pedaço do código informa qual pagina ira ser mapeada
    @Override
    protected Class<?>[] getServletConfigClasses() {

        return new Class[]{AppWebConfiguration.class};
    }


    // esse código mapea o projeto
    @Override
    protected String[] getServletMappings() {
        // TODO Auto-generated method stub
        return new String[] {"/"};
    }

part2;

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


    //esse pedaço do código informa aonde está a pagina dentro pacote especifico

    @Bean
    public InternalResourceViewResolver 
        internalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

part3;

@Controller
public class HomeController {

    @RequestMapping("/")
    public String index(){

        System.out.println("carregando produto");
        return "hello-world";// essa é a pagina que está sendo visualizada
    }

1 answer

1


Good afternoon, have a look at your spring.xml file configuration and whether you have configured Welcome-list-file on web.xml, but if the configuration has been done by annotated or extended classes (Beans) take a look if in the spring mvc configuration class you have configured the Sufix and the prefix it will inform to the server to interpret and if you have configured the Welcome file for what you want.

Okay, I get it now, in case you need to add the page you want to upload to the views folder, to your controller when you put the request mapping ("/") he is saying that every request made to the url http://localhost:8080/Loja/ will be handled by that class and in the method as the return is a string, this return represents the name of the view that after merging with the prefix .jsp that has been configured in the class AppWebConfiguration will be hello-world.jsp, that is, it will click on the screen the view returned by the method, if you want to upload this view by another just implement a new method with another request Mapping or just change the return to the name of the page you want to load on the screen, if you have an index.jsp page that wants to load it must be inside the views folder and at the return of the method you must inform the return as index.

  • I just updated my post and put notes in the code for you to understand, take a look please to help me better

Browser other questions tagged

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