Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc ViewResolver在Spring 4中工作不正常_Spring Mvc_Model View Controller - Fatal编程技术网

Spring mvc ViewResolver在Spring 4中工作不正常

Spring mvc ViewResolver在Spring 4中工作不正常,spring-mvc,model-view-controller,Spring Mvc,Model View Controller,我的ViewResolver未按预期工作。我有多个控制器来处理不同的请求。以下是代码片段: AppConfig类 package in.andonsystem.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.

我的ViewResolver未按预期工作。我有多个控制器来处理不同的请求。以下是代码片段:

AppConfig类

package in.andonsystem.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@ComponentScan(basePackages = {"in.andonsystem"})
public class AppConfig extends WebMvcConfigurerAdapter{

} 
DispatcherConfig类:

package in.andonsystem.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;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"in.andonsystem.controllers"})
public class DispatcherConfig 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/");
   }
}
应用初始化器类

package in.andonsystem.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class AppInitializer implements WebApplicationInitializer{

   @Override
   public void onStartup(ServletContext servletContext) throws ServletException {
      // Create the 'root' Spring application context
      AnnotationConfigWebApplicationContext rootContext =
        new AnnotationConfigWebApplicationContext();
      rootContext.register(AppConfig.class);

      // Manage the lifecycle of the root application context
      servletContext.addListener(new ContextLoaderListener(rootContext));
      servletContext.addListener(ContextListener.class);

      // Create the dispatcher servlet's Spring application context
      AnnotationConfigWebApplicationContext dispatcherContext =
        new AnnotationConfigWebApplicationContext();
      dispatcherContext.register(DispatcherConfig.class);

      // Register and map the dispatcher servlet
      ServletRegistration.Dynamic dispatcher =
        servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
      dispatcher.setLoadOnStartup(1);
      dispatcher.addMapping("/");
   }
}
混合控制器类

package in.andonsystem.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MiscController {

   @RequestMapping("/")
   public String index(){
      return "index";
   }
}
package in.andonsystem.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/section")
public class SectionController {

   @RequestMapping(name = "/show",method = RequestMethod.GET)
   public String showSection(){
      System.out.println("Section");
      return "section";
   }

}
SectionController类

package in.andonsystem.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MiscController {

   @RequestMapping("/")
   public String index(){
      return "index";
   }
}
package in.andonsystem.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/section")
public class SectionController {

   @RequestMapping(name = "/show",method = RequestMethod.GET)
   public String showSection(){
      System.out.println("Section");
      return "section";
   }

}
我面临的问题是,当我访问url
{context path}/section/show
时,它被重定向到SectionController的showssection()方法,“section”被打印在标准输出上,但随后它被转发到
{root context}/section/WEB-INF/views/section.jsp
而不是
{root context}/WEB-INF/views/section.jsp

我错过了什么

提前谢谢。

我想应该是

viewResolver.setPrefix("/WEB-INF/views/");
否则,转发是相对于请求URL的。

我认为应该是这样

viewResolver.setPrefix("/WEB-INF/views/");
否则,转发是相对于请求URL的