【发布时间】:2019-03-12 14:53:41
【问题描述】:
网站和网络上通常有几个类似的问题,但我无法像我尝试过的那样让它们在我的示例中发挥作用。
我是第一次使用 Spring Boot,但我一直在尝试通过 InternalResourceViewResolver 包含 JSP 视图。我已经让 Thymeleaf 视图工作了。
Application.java
@SpringBootApplication
@ComponentScan("controller")
@EnableWebSecurity
@Configuration
public class Application extends WebSecurityConfigurerAdapter {
public static void main(String args[]) {
SpringApplication.run(Application.class, args);
}
@Bean
public ITemplateResolver templateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setPrefix("classpath:/templates/");
resolver.setSuffix(".html");
resolver.setTemplateMode(TemplateMode.HTML);
resolver.setCharacterEncoding("UTF-8");
resolver.setCacheable(false);
resolver.setOrder(1);
return resolver;
}
//intended for the .jsp view
@Bean
public InternalResourceViewResolver jspResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("classpath:/templates/jsp/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
resolver.setOrder(2);
return resolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addTemplateResolver(new UrlTemplateResolver());
templateEngine.addTemplateResolver(templateResolver());
//IKD if/how I should somehow add jspResolver() here
templateEngine.addDialect(new SpringSecurityDialect());
templateEngine.addDialect(new LayoutDialect(new GroupingStrategy()));
templateEngine.addDialect(new Java8TimeDialect());
return templateEngine;
}
@Bean
@Description("Thymeleaf View Resolver")
public ThymeleafViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setOrder(0);
return viewResolver;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/users/**").hasRole("USER")//USER role can access /users/**
.antMatchers("/admin/**").hasRole("ADMIN")//ADMIN role can access /admin/**
.antMatchers("/quests/**").permitAll()// anyone can access /quests/**
.anyRequest().authenticated()//any other request just need authentication
.and()
.formLogin();//enable form login
}
@Override
protected void configure(AuthenticationManagerBuilder builder) throws Exception {
builder.inMemoryAuthentication()
.passwordEncoder(passwordEncoder())
.withUser("tim").password(passwordEncoder().encode("123")).roles("ADMIN")
.and()
.withUser("joe").password(passwordEncoder().encode("234")).roles("USER");
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
MainController.java
@Controller
public class MainController {
@GetMapping("/")
ModelAndView index(Principal principal) {
ModelAndView mv = new ModelAndView("home");
if (principal != null) {
mv.addObject("message", principal.getName());
} else {
mv.addObject("message", "anon.");
}
return mv;
}
@GetMapping("/**")
String request(HttpServletRequest request, Model model) {
Authentication auth = SecurityContextHolder.getContext()
.getAuthentication();
ModelAndView mv = new ModelAndView("home");
model.addAttribute("uri", request.getRequestURI())
.addAttribute("user", auth.getName())
.addAttribute("roles", auth.getAuthorities());
return "html"; //<-- whenever I change this to return "jsp/jsp"; it breaks
}
html.html(百里香叶)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<body>
<p>
URI: <h3 th:text="${uri}"></h3>
User: <h3 th:text="${user}"></h3>
Roles: <h3 th:text="${roles}"></h3>
<a href="/admin/">/admin/</a><br/>
<a href="/users/">/users/</a><br/>
<a href="/others/">/others/</a><br/>
<a href="/quests/">/quests/</a><br/><br/>
</p>
<form th:action="@{/logout}" method="post">
<input type="hidden"
name="${_csrf.parameterName}"
value="${_csrf.token}"/>
<input type="submit" value="Logout">
</form>
</body>
</html>
当我尝试用 JSP 文件很好地完成这项工作时,浏览器只输出
HTTP 状态 500 ?内部服务器错误
在 NetBeans Output 窗口中,gradle 的任务正在运行,日志显示在最顶部(整个日志非常广泛):
2018-10-07 18:09:40.070 错误 6024 --- [nio-8080-exec-4] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-4]异常处理模板“jsp/jsp”:模板解析时出错(模板:“类路径资源[templates/jsp/jsp.html]”)
我正在尝试包含的 JSP 视图
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html lang="en">
<body>
<p>URI: ${uri} <br/>
User : ${user} <br/>
roles: ${roles} <br/><br/>
<a href="/admin/">/admin/</a><br/>
<a href="/users/">/users/</a><br/>
<a href="/others/">/others/</a><br/>
<a href="/quests/">/quests/</a><br/><br/>
</p>
<form action="/logout" method="post">
<input type="hidden"
name="${_csrf.parameterName}"
value="${_csrf.token}"/>
<input type="submit" value="Logout">
</form>
</body>
</html>
最后,我的项目树:
我的假设是应用程序不知道文件夹 templates/jsp 中的文件 jsp.jsp,这就是为什么我将问题的目标是查看解析器,但正如我所说,我很容易就错了。
这只是我试图具体化和构建的一个示例,因此请随时提出建议,谢谢。
【问题讨论】:
-
如果您正在开始新项目,为什么要使用 jsp.和百里香一起去。
-
@want2learn;我以前听说过,是的,我会选择百里香,但我只是 want2learn
-
Spring boot 不鼓励使用带有嵌入式 tomcat 的 jsp。但是如果你想要2learn检查github.com/spring-projects/spring-boot/tree/v2.0.5.RELEASE/…
-
我会的,谢谢链接,但是,真的没有办法使用 JSP没有 webapp/WEB-INF 结构,但只能使用资源/模板之一? @want2learn
-
由于我没有将jsp与spring boot一起使用,我不能从我的经验中说,但是spring boot docs说将你的jsps放在webapp / WEB-INF结构上
标签: spring-mvc jsp spring-boot jstl thymeleaf