I
I
ilavio2020-10-17 17:15:53
Java
ilavio, 2020-10-17 17:15:53

Java Spring MVC doesn't see views, why?

Good afternoon!
I am learning java. Moved to spring. Rested against the wall, for 1.5 months now. I work through Eclipse. downloaded tomcat. I opened a video about java spring MVC, started to repeat after the teacher, he worked in Intel idea. As a result, my web application only opens the index.isp file created by maven.
Okay, I think I made a joint, I'll try another lesson. I found a site where they make simple Spring MVC step by step. Stupidly I copy each action step by step. The result is the same. Google, read that if the controller is not configured correctly, then he is looking for an alternative to display, AHA! I delete this index.jsp and the same page is DISPLAYED! Page which is not in the project! Other views do not see!
What needs to be configured? Or fix the code? Which way to breathe?
Posted everything on Github: https://github.com/ilavio/SpringMVC_lesson3

https://github.com/ilavio/SpringLesson2
For testing convenience.

Here is the code of the main classes:
Configuration class:

package com.javabycode.config;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;


@SuppressWarnings("deprecation")
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "springmvc.src.main.java.com.javabycode")
public class MyWebConfig extends WebMvcConfigurerAdapter {
  
  @Bean
  public ViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
  } //
  
  
  @Bean
  public MessageSource messageSource() {
      ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
      messageSource.setBasename("messages");
      return messageSource;
  }
 
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
      registry.addResourceHandler("/static/**").addResourceLocations("/static/");
  }
  
}


Servlet class:
package com.javabycode.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class ServletInitializer implements WebApplicationInitializer {

  public void onStartup(ServletContext container) throws ServletException {
    // TODO Auto-generated method stub
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(MyWebConfig.class);
    ctx.setServletContext(container);
 
    ServletRegistration.Dynamic servlet = container.addServlet(
        "dispatcher", new DispatcherServlet(ctx));
 
    servlet.setLoadOnStartup(1);
    servlet.addMapping("/");
  }
  
  
  
}


Controller class:
package com.javabycode.springmvc;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.javabycode.model.Employee;



@Controller
@RequestMapping("/")
public class MyController {
  
  /*
   * This method will serve as default GET handler.
   */
  @RequestMapping(method = RequestMethod.GET)
  public String newProfile(ModelMap model) {
    Employee employee = new Employee();
    model.addAttribute("employee", employee);
    return "employee";
  }
 
  /*
   * This method will be called on form submission, handling POST request It
   * also validates the user input
   */
  @RequestMapping(method = RequestMethod.POST)
  public String saveProfile(Employee employee,
      BindingResult result, ModelMap model) {
 
    if (result.hasErrors()) {
      return "employee";
    }
 
    model.addAttribute("success", "Dear " + employee.getFirstName()
        + " , your profile completed successfully");
    model.addAttribute("employee",employee);
    return "success";
  }
 
 
  /*
   * Method used to populate the country list in view. Note that here you can
   * call external systems to provide real data.
   */
  @ModelAttribute("countries")
  public List<String> initializeCountries() {
 
    List<String> countries = new ArrayList<String>();
    countries.add("USA");
    countries.add("Canada");
    countries.add("France");
    countries.add("Indonesia");
    countries.add("Australia");
    countries.add("Other");
    return countries;
  }

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
ilavio, 2021-02-10
@ilavio

All ALLOWED! This question (less than a year later): When I began to re-watch the Spring lessons, I thought, what if the problems are not in my code, but in the wrong configuration of Eclipse or the built-in Tomcat. And Installed another JBOSS server (WildFly); I set it up (I did NOT connect it to Eclipse), climbed into the project, got into the target folder and THERE is no WAR archive. Either I don’t understand something, or this system should somehow work differently (After all, the .war archive is needed to work in the server). Well, it's good that I installed regular maven. I opened the command line and went to the project. Gave the command to create a .war archive " mvn package ". I put this archive into the server folder wildfly-22.0.0.Final\standalone\deployments .
Launched the server: for Windows it is a file - standalone.bat. And voila everything worked! My project has started!
After I tried to connect WildFly to Eclipse and what do you think. This program (Eclipse) does not want to install the JBOSS tool to work with the WildFly server. In general, some kind of tryndets! Resist! I will manually install .war archives into the server

Z
zdezak, 2020-10-18
@zdezak

A couple of errors in the MyConfig.java file
[email protected](basePackages = "springmvc.src.main.java.com.javabycode"). It should be from the Java folder and look like this - @ComponentScan(basePackages = "com.javabycode").
2. Line viewResolver.setPrefix("/WEB-INF/views/"); on GitHub looks like viewResolver.setPrefix("/WEB-INF/views/*");.
The asterisk at the end is not needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question