如何获得例如registration.jsp视图在浏览器中正确显示吗?

时间:2019-05-04 13:31:19

标签: spring-boot spring-mvc jsp intellij-idea view

我正在按照本教程的内容在Spring Boot Web应用程序中进行登录和注册:https://hellokoding.com/registration-and-login-example-with-spring-security-spring-boot-spring-data-jpa-hsql-jsp/

我无法正确显示视图-而不是例如在registration.jsp视图页面上,我在屏幕上看到以下字符串,该字符串由我的GET注册控制器方法返回:https://ibb.co/XXXtdSZ

我是Java,Spring Boot和JSP的新手。 我正在使用IntelliJ Community Edition 2017.3.5,并且我收到不支持.jsp文件的通知。之后,https://stackoverflow.com/a/36572413/11451547,我进入了“设置-文件类型-HTML”下的“可识别的文件类型”,并添加了一个新的注册模式。* jsp,覆盖了JSP文件已经存在的已注册模式(仅语法突出显示)。不支持.jsp的消息消失了,但是我仍然看不到查看页面。

在我的registration.jsp中,我看到了很多“命名空间,例如,“表单”未绑定”通知。在具有相同显示问题(在屏幕上,我看到字符串'login'而不是登录视图)的login.jsp中,我有一个通知-'名称空间'c'未绑定'。

您能提供一些有关如何正确显示视图的想法吗?

谢谢!

这是我控制器的各个部分(也许我在向GET注册两次添加@ModelAttribute时是错误的,这是我对教程代码所做的修改的一部分,但是例如,我的GET登录控制器方法与教程):

@GetMapping("/registration")
    public String registration(Model model) {
        model.addAttribute("userForm", new User());

        return "registration";
    }

@PostMapping("/registration")
    public String registration(@ModelAttribute("userForm") User userForm, @ModelAttribute("roleId") Long roleId, BindingResult bindingResult) { //diff w tutorial
        userValidator.validate(userForm, bindingResult);
//        userValidator.validate(roleId, bindingResult);

        if (bindingResult.hasErrors()) {
            return "registration";
        }
        userService.saveUser(userForm, roleId);

        securityService.autoLogin(userForm.getUserName(), userForm.getPasswordConfirm());

        return "redirect:/welcome";
    }

    @GetMapping("/login")
    public String login(Model model, String error, String logout) {
        if (error != null)
            model.addAttribute("error", "Your username and/or password is invalid.");

        if (logout != null)
            model.addAttribute("message", "You have been logged out successfully.");

        return "login";
    }

    @GetMapping({"/", "/welcome"})
    public String welcome(Model model) {
        return "welcome";
    }

我的registration.jsp:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<c:set var="contextPath" value="${pageContext.request.contextPath}">

<!DOCTYPE html>
<html lang="en">
  <head>
      <meta charset="utf-8">
      <title>Create an account</title>

      <link href="${contextPath}/resources/css/bootstrap.min.css" rel="stylesheet">
      <link href="${contextPath}/resources/css/common.css" rel="stylesheet">
  </head>

  <body>
    <div class="container">
        <form:form method="POST" modelAttribute="userForm" modelAttribute="roleId" class="form-signin">
            <h2 class="form-signin-heading">Create Your Account</h2>
            <spring:bind path="userName">
                <div class="form-group ${status.error ? 'has-error' : ''}">
                    <form:input type="text" path="userName" class="form-control" placeholder="Enter your username"
                                autofocus="true"></form:input>
                    <form:errors path="userName"></form:errors>
                </div>
            </spring:bind>

            <spring:bind path="password">
                <div class="form-group ${status.error ? 'has-error' : ''}">
                    <form:input type="password" path="password" class="form-control" placeholder="Enter your password"></form:input>
                    <form:errors path="password"></form:errors>
                </div>
            </spring:bind>

            <spring:bind path="passwordConfirm">
                <div class="form-group ${status.error ? 'has-error' : ''}">
                    <form:input type="password" path="passwordConfirm" class="form-control"
                                placeholder="Confirm your password"></form:input>
                    <form:errors path="passwordConfirm"></form:errors>
                </div>
            </spring:bind>

            <button class="btn btn-lg btn-primary btn-block" type="submit">Submit</button>
        </form:form>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script src="${contextPath}/resources/js/bootstrap.min.js"></script>
  </body>
</html>

我的login.jsp:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<c:set var="contextPath" value="${pageContext.request.contextPath}"/>

<!DOCTYPE html>
<html lang="en">
  <head>
      <meta charset="utf-8">
      <title>Log in with your account details</title>

      <link href="${contextPath}/resources/css/bootstrap.min.css" rel="stylesheet">
      <link href="${contextPath}/resources/css/common.css" rel="stylesheet">
  </head>

  <body>
    <div class="container">
      <form method="POST" action="${contextPath}/login" class="form-signin">
        <h2 class="form-heading">Log In</h2>
        <div class="form-group ${error != null ? 'has-error' : ''}">
            <span>${message}</span>
            <input name="userName" type="text" class="form-control" placeholder="Enter your username"
                   autofocus="true"/>
            <input name="password" type="password" class="form-control" placeholder="Enter your password"/>
            <span>${error}</span>
            <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>

            <button class="btn btn-lg btn-primary btn-block" type="submit">Log In</button>
            <h4 class="text-center"><a href="${contextPath}/registration">Create an Account</a></h4>
        </div>
      </form>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script src="${contextPath}/resources/js/bootstrap.min.js"></script>
  </body>
</html>

我的welcome.jsp:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:set var="contextPath" value="${pageContext.request.contextPath}"/>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Welcome</title>

    <link href="${contextPath}/resources/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>
  <div class="container">
    <c:if test="${pageContext.request.userPrincipal.name != null}">
        <form id="logoutForm" method="POST" action="${contextPath}/logout">
            <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
        </form>

        <h2>Welcome ${pageContext.request.userPrincipal.name} | <a onclick="document.forms['logoutForm'].submit()">Logout</a></h2>
    </c:if>
  </div>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
  <script src="${contextPath}/resources/js/bootstrap.min.js"></script>
</body>
</html>

application.yml文件的各个部分:

spring:
  main:
    banner-mode: 'off'
  datasource:
    url: jdbc:mysql://localhost:3306/inspire_me_db?useSSL=false
    username: springuser
    password: ThePassword
  jpa:
    hibernate:
      ddl-auto: update
  flyway:
    baselineOnMigrate: true
  mvc:
    view:
      prefix: /
      suffix: .jsp
  messages:
    basename: validation

我的pom.xml的相应部分:

<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
</dependency>

我的包裹结构:

https://ibb.co/Qr2dmNr

1 个答案:

答案 0 :(得分:0)

您可以尝试如下注册jsp视图解析器吗:

@Configuration
@EnableWebMvc
@ComponentScan
public class MvcConfiguration implements WebMvcConfigurer
{
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        registry.viewResolver(resolver);
    }
}

或者您也可以通过在application.properties或yml中提供以下内容,将jsp指定为Spring-boot中的视图解析器:

spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp

验证提供的前缀指向jsp文件所在的正确目录。通常,将jsp文件放在src/main/WEB_INF/view下。

另外,由于您使用的是spring-boot,如果没有适当的依赖项来编译jsp,请在pom中添加以下内容:

<!-- JSTL -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
<!-- To compile JSP files -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>