为什么我的JSTL if-check不起作用?

时间:2016-11-15 21:09:52

标签: spring-mvc spring-security jstl conditional-statements

我正在尝试使用Spring Security创建具有登录功能的简单应用程序。但我无法达到预期的效果。 我的.jsp页面上的JSTL标签没有通过测试,而scriplet代码在我的应用程序中应该避免。

我的JSP页面。

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
    <title>Log in page</title>
</head>
<body>
<form action="<c:url value="/login"/>" method="POST">
    <p>
        <label for="username">Username</label>
        <input type="text" id="username" name="username"/>
    </p>
    <p>
        <label for="password">Password</label>
        <input type="password" id="password" name="password"/>
    </p>
    <button type="submit" class="btn">Log in</button>
</form>

// This block of code is never executed
<c:if test="${error != null}">
    Some test message
</c:if>

// While this one works fine
<%
    if (request.getParameter("error") != null) {
        out.write("error's value isn't null\n");
    }
%>
</body>
</html>

WebSecurityConfigurerAdapter覆盖配置方法:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();

        http.antMatchers("/product")
            .access("hasRole('ROLE_MANAGER')");

        http.authorizeRequests().and().formLogin()
            .loginPage("/login")
            .defaultSuccessUrl("/admin")
            .failureUrl("/login?error")
            .usernameParameter("username")
            .passwordParameter("password")
            .and().logout().logoutSuccessUrl("/login?logout");
}

Spring MVC Controller的映射“/ login”请求的方法。

@RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginPage(Model model,
                            @RequestParam(value = "error", required = false) String error {
        if (error != null) {
            model.addAttribute("error", "Username or password is incorrect.");
        }

        return "login";
    }

这就是我要求的http://localhost/login?error

Image

1 个答案:

答案 0 :(得分:0)

我已经改变了

<%@ page contentType="text/html;charset=UTF-8" language="java"%>

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>

据我了解,EL评估已停用,因此我手动激活它。

相关问题