我在RestAppConfig
中声明了UrlBasedViewResolver方法来渲染jsp页面。
@Bean
public UrlBasedViewResolver urlBasedViewResolver(){
UrlBasedViewResolver res = new InternalResourceViewResolver(); //new UrlBasedViewResolver();//
res.setOrder(10);
res.setViewClass(JstlView.class);
res.setPrefix("/views/templates/");
res.setSuffix(".jsp");
return res;
}
并在控制器BaseController
@Configuration
@EnableWebMvc
@ComponentScan(basePackages={"kr.co.cdefi.base.restapp.controller"}
, useDefaultFilters=false
, includeFilters={@Filter(Controller.class)
, @Filter(ControllerAdvice.class)})
public class RestAppConfig extends WebMvcConfigurerAdapter{
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer
.useJaf(true)
.favorPathExtension(true)
.favorParameter(false)
.ignoreAcceptHeader(false)
.defaultContentType(MediaType.APPLICATION_JSON)
.mediaType("json", MediaType.APPLICATION_JSON)
.mediaType("xml", MediaType.APPLICATION_XML);
};
@Bean
public UrlBasedViewResolver urlBasedViewResolver(){
UrlBasedViewResolver res = new InternalResourceViewResolver(); //new UrlBasedViewResolver();//
res.setOrder(10);
res.setViewClass(JstlView.class);
res.setPrefix("/views/templates/");
res.setSuffix(".jsp");
return res;
}
}
最后,我使用Eclipse运行tomcat8服务器。
但是请求的URI /home
只是回应了这个。
警告o.s.web.servlet.PageNotFound - 在DispatcherServlet中找不到带有URI [/TheBigs/views/templates/index.jsp]的HTTP请求的映射名称' rest-app'
我打开了部署位置
...\org.eclipse.wst.server.core\tmp1\wtpwebapps\TheBigs\views\templates\
...文件存在。问题是什么?我有什么遗漏的吗?
我添加了控制器代码BaseController
@Controller public class BaseController { Logger logger = LoggerFactory.getLogger(BaseController.class);
@Autowired UserService userService;
@RequestMapping(value="/home", method = RequestMethod.GET)
public ModelAndView home(){
logger.debug("home!");
ModelAndView view = new ModelAndView();
view.setViewName("index");
return view;
}
@RequestMapping(value="/", method = RequestMethod.GET)
public String index(Model model){
logger.debug("index page!");
model.addAttribute("message", "success");
return "test";
}
}
答案 0 :(得分:2)
解决。它由servlet-mapping
引起。
我将url-pattern
设置为/
/*
,然后才开始工作。