开始使用Spring MVC,当我进入我的页面时,它将welcome.jsp作为纯文本加载(只显示源代码),我知道有人问过这一百万次,但我查看过很多问题而没有找到我的解决方案,因为大多数人都使用XML而且我使用java ..我仍然没有足够的经验在两者之间切换。
conf.class:
public class conf extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public InternalResourceViewResolver jspViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/views/");
bean.setSuffix(".jsp");
return bean;
}
}
ServletInitializer.class:
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Demo4Application.class,conf.class);
}
}
welCont.class(Controller):
@Controller
@RequestMapping("/spring/")
public class welCont {
@RequestMapping(method = RequestMethod.GET)
public String wel(ModelMap model)
{
model.addAttribute("test","testme");
return "welcome";
}
}
welcome.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>${test}</h2>
</body>
</html>
答案 0 :(得分:0)
我认为错误的是
@RequestMapping("/spring/")
通常“/ spring /”请求应该转到DispatcherServlet。春季靴子不确定。
试试这个。
@Controller
public class WelCont {
@RequestMapping(value="/foldername_if_avaiable/welcome/", method=RequestMethod.GET )
public ModelAndView wel(
@RequestParam(value="id", required=false) String id,
ModelMap model,
HttpSession session,
HttpServletRequest req) throws Exception {
model.addAttribute("test","testme");
return new ModelAndView("/foldername_if_avaiable/welcome", model);
}
}