开发者社区> 问答> 正文

找到但未呈现Spring Boot 2.1.x JSP文件

我目前正在将一个小型应用程序迁移到SpringBoot。我们有旧版代码,我希望通过将登录页面连接到SpringBoot中,可以让Web应用程序的JSP / JS文件处理其余部分。

目前,我已经成功地将我们升级到Spring 5,并使用SpringBoot 2.1.8。

POM片段:该应用程序使用的是Spring提供的Tomcat默认值,并且引入了相关的Jasper / JSTL JARS,以便Tomcat可以读取

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-activemq</artifactId>
        <exclusions>
            <exclusion>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

根据此DZone文章,我构建了一个小型控制器来处理登录过程

@Controller public class LoginController {

@Resource
private UserSessionService userSessionService;

@GetMapping(value = "/")
public String index() {
    Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "LoginController: Hit /, returning index.jsp");
    return "index";
}

@GetMapping(value = "/login")
public String login() {
    Logger.getLogger(this.getClass().getName())
          .log(Level.SEVERE, "LoginController: Hit /login, returning login.jsp");
    return "login";
}

@GetMapping(value = "/loginFailed")
public String loginError(Model model) {
    Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "LoginController: Failed to login");
    model.addAttribute("error", "true");
    return "login";
}

@GetMapping(value = "/logout")
public String logout(SessionStatus session) {
    Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "LoginController: Logging out");
    SecurityContextHolder.getContext().setAuthentication(null);
    session.setComplete();
    return "redirect:/login";
}

@PostMapping(value = "/postLogin")
public String postLogin(Model model, HttpSession session) {
    Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "LoginController: postLogin");
    return "redirect:/";
}

} login.jsp:旧版代码,已修改以调用控制器。我正在尽最大努力让Spring Security处理调用AuthenticaionProvider和DB的过程(为简洁起见,我仅显示关键区域内容,无论如何该页面都非常简单

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %> <c:set var="contextPath" value="${pageContext.request.contextPath}"/>

<%@page contentType="text/html" pageEncoding="UTF-8" %> . .
Login
Password

. . index.jsp:旧版代码,处理主登录页面

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <c:set var="contextPath" value="${pageContext.request.contextPath}"/> <%@include file="beforeHtml.jsp" %>

<%@include file="head.jsp" %> <%@include file="top.jsp" %>

Release Notes

我有一个名为SecurityConfig的文件,让SpringBoot知道绕过“默认”登录页面:

@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests().antMatchers("/xxx").hasAnyRole("ADMIN", "USER")
            .and()
            .authorizeRequests().antMatchers("/login", "/resource/**").permitAll()
            .and()
            .formLogin().loginPage("/login").usernameParameter("username").passwordParameter("password").permitAll()
            .loginProcessingUrl("/doLogin")
            .successForwardUrl("/postLogin")
            .failureUrl("/loginFailed")
            .and()
            .logout().logoutUrl("/doLogout").logoutSuccessUrl("/logout").permitAll()
            .and()
            .csrf().disable();
}

我的application.properties文件中有这些字段

server.port=8888 server.servlet.context-path=/xxx server.tomcat.additional-tld-skip-patterns=mchange-commons-java.jar

spring.mvc.view.prefix=/WEB-INF/view/jsp/ spring.mvc.view.suffix=.jsp spring.mvc.servlet.load-on-startup=1 所有这些加在一起,使我可以按要求的URL登录localhost:8888/xxx/login就可以了,然后登录并观看Spring发现我的设置并登录的日志。问题在于何时要获取名为的JSP文件的登录页面index.jsp。根据SpringBoot / Application的记录

Oct 09, 2019 2:44:37 PM org.springframework.core.log.LogFormatUtils traceDebug FINE: GET "/xxx/login", parameters={} Oct 09, 2019 2:44:37 PM org.springframework.web.servlet.handler.AbstractHandlerMapping getHandler FINE: Mapped to public java.lang.String com.rhdjapan.xxx.controllers.LoginController.login() Oct 09, 2019 2:44:37 PM com.xx.xxx.controllers.LoginController login SEVERE: LoginController: Hit /login, returning login.jsp Oct 09, 2019 2:44:37 PM org.springframework.web.servlet.view.ContentNegotiatingViewResolver getBestView FINE: Selected 'text/html' given [text/html,...] Oct 09, 2019 2:44:37 PM org.springframework.web.servlet.view.AbstractView render FINE: View name 'login', model {} Oct 09, 2019 2:44:37 PM org.springframework.web.servlet.view.InternalResourceView renderMergedOutputModel FINE: Forwarding to [/WEB-INF/view/jsp/login.jsp] Oct 09, 2019 2:44:38 PM org.springframework.web.servlet.FrameworkServlet logResult FINE: Completed 200 OK Oct 09, 2019 2:44:42 PM com.xx.xxx.services.common.security.service.UserDetailsServiceImpl loadUserByUsername SEVERE: loadUserByUsername: looking up user [admin] Oct 09, 2019 2:44:42 PM com.xx.xxx.services.common.security.service.UserDetailsServiceImpl loadUserByUsername SEVERE: loadUserByUsername: userId found = [1111111] Oct 09, 2019 2:44:42 PM org.springframework.core.log.LogFormatUtils traceDebug FINE: "FORWARD" dispatch for POST "/xxx/postLogin", parameters={username:[admin], password:[admin]} Oct 09, 2019 2:44:42 PM org.springframework.web.servlet.handler.AbstractHandlerMapping getHandler FINE: Mapped to public java.lang.String com.xx.xxx.controllers.LoginController.postLogin(org.springframework.ui.Model,javax.servlet.http.HttpSession) Oct 09, 2019 2:44:42 PM com.xx.xxx.controllers.LoginController postLogin SEVERE: LoginController: postLogin Oct 09, 2019 2:44:42 PM org.springframework.web.servlet.view.AbstractView render FINE: View name 'redirect:', model {} Oct 09, 2019 2:44:42 PM org.springframework.web.servlet.FrameworkServlet logResult FINE: Exiting from "FORWARD" dispatch, status 302 Oct 09, 2019 2:44:42 PM org.springframework.core.log.LogFormatUtils traceDebug FINE: GET "/xxx/", parameters={} Oct 09, 2019 2:44:42 PM org.springframework.web.servlet.handler.AbstractHandlerMapping getHandler FINE: Mapped to public java.lang.String com.xx.xxx.controllers.LoginController.index() Oct 09, 2019 2:44:42 PM com.xx.xxx.controllers.LoginController index SEVERE: LoginController: Hit /, returning index.jsp Oct 09, 2019 2:44:42 PM org.springframework.web.servlet.view.ContentNegotiatingViewResolver getBestView FINE: Selected 'text/html' given [text/html, ...] Oct 09, 2019 2:44:42 PM org.springframework.web.servlet.view.AbstractView render FINE: View name 'index', model {} Oct 09, 2019 2:44:42 PM org.springframework.web.servlet.view.InternalResourceView renderMergedOutputModel FINE: Forwarding to [/WEB-INF/view/jsp/index.jsp] Oct 09, 2019 2:44:42 PM org.springframework.web.servlet.FrameworkServlet logResult FINE: Completed 200 OK

这就是问题所在:成功找到JSP文件时,不会呈现页面。我返回了一个空的HTML文件,使用Web调试器(chrome)仅显示15个空白行的来源。我认为它是我index.jsp文件的长度;

我的问题是双重的

HTML / JSP无法在屏幕上呈现的原因可能是什么? 如果有修复,对于我在SpringBoot下需要工作的所有其他旧版JSP文件,它们是否都需要类似的过程?

更新 为简洁起见,有人问起我制作的WAR的结构,当我拉起档案时,结构就是这样

WAR |____css |____dashboard_view |____excel |____images |____jquery |____js |____jsp |____META-INF |____org |____third-party |____WEB-INF |________classes |________lib |________view |____________html |____________jsp |_________________index.jsp |_________________login.jsp 考虑到我提供的登录和索引jsp文件,我想知道在渲染过程中某个地方是失败了吗?

展开
收起
小六码奴 2019-10-09 19:47:18 1013 0
1 条回答
写回答
取消 提交回答
  • 似乎是servlet无法找到某些文件的情况,因此未渲染任何内容,因为它找不到任何内容 我需要进一步核实。但在移动home.js下的WEB-INF/view/js/home每个索引。我开始从我的网页中获取一些堆栈跟踪

    Exception: org.apache.jasper.JasperException: java.lang.NullPointerException

    Stack Trace org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:516) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:712) org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:580) org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:516) org.springframework.security.web.header.HeaderWriterFilter$HeaderWriterRequestDispatcher.include(HeaderWriterFilter.java:149) org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:955) org.apache.jsp.WEB_002dINF.view.jsp.index_jsp._jspService(index_jsp.java:150) 对于我在SpringBoot中的登录

    SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [/xxx] threw exception [java.lang.NullPointerException] with root cause java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Properties.java:434) at java.util.Properties.load0(Properties.java:353) at java.util.Properties.load(Properties.java:341) at com.xx.xxx.common.utils.common.GenericPropertiesUtil.loadProperties(GenericPropertiesUtil.java:16) at com.xx.xxx.common.utils.common.EnvPropertiesUtil.getEnvironmentProperties(EnvPropertiesUtil.java:43) at com.xx.xxx.common.utils.common.EnvPropertiesUtil.getParameterValue(EnvPropertiesUtil.java:23) at com.xx.xxx.common.utils.common.EnvPropertiesUtil.getFlagParameterValue(EnvPropertiesUtil.java:37) at com.xx.xxx.common.utils.Config.getFlagValue(Config.java:131) at com.xx.xxx.common.utils.Config.isProd(Config.java:59) at com.xx.xxx.common.utils.IO.debug(IO.java:514)

    这似乎表明该页面上现在有活动

    如果需要,我将进行验证并重新打开。

    2019-10-09 19:50:54
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多