【发布时间】:2016-08-29 21:09:29
【问题描述】:
开始使用 Spring MVC,当我进入我的页面时,它会将welcome.jsp 加载为纯文本(仅显示源代码),我知道人们问了这 100 万次,但我查看了很多问题并没有找到我的解决方案,因为他们中的大多数都使用 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
@RequestMapping("/spring/")
public class welCont {
@RequestMapping(method = RequestMethod.GET)
public String wel(ModelMap model)
{
model.addAttribute("test","testme");
return "welcome";
}
}
欢迎.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>${test}</h2>
</body>
</html>
【问题讨论】:
标签: spring jsp spring-mvc