【问题标题】:Parsing Spring EL Variable in View在视图中解析 Spring EL 变量
【发布时间】:2015-12-04 00:26:09
【问题描述】:

我有一个用 Spring EL 编写的变量,我想对它进行 url 解码,我可以在大括号之间插入一段代码还是必须在控制器中完成解析?

<form:hidden id="pass" path="password" value="${param.password}" htmlEscape="true" />

例如,我可以这样做吗:

<form:hidden id="pass" path="password" value="${URLDecoder.decode(param.password, "UTF-8")}" htmlEscape="true" />

问题是某些密码包含特殊字符并且是 url 编码的。

【问题讨论】:

    标签: java spring spring-el


    【解决方案1】:

    尝试使用spring:eval 执行静态方法调用并设置值。参考资料:

    Spring Eval Reference

    Spring Eval Example 1

    更新:

    Spring Eval Example 2

    更新: 尝试了您正在寻找的东西,这就是我所拥有的:

    控制器

    @RequestMapping("/testStaticCall")
        public String testStaticCall(ModelMap map) {
            map.addAttribute("password", "1234%23");
            return "testStaticCallJsp";
        }
    

    JSP

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8" isELIgnored="false" %>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <title>Test</title>
    </head>
    <body>
        <spring:eval expression="T(java.net.URLDecoder).decode(password)" var="decodedPassword"></spring:eval>
        <form:input id="pass" path="password" value="${decodedPassword}" htmlEscape="true" />
    </body>
    </html>
    

    输出

    【讨论】:

    • 这样的? &lt;spring:eval expression="T(java.net.URLDecoder).decode(param.password)" var="decodedPassword"&gt;&lt;/spring:eval&gt; 然后使用decodedPassword 作为新值?
    • 不,似乎无法解码非字母数字字符。
    • 您能否将控制器端的返回值与生成的值进行比较?刚才提到了一种调用静态方法的方法。
    • 你指的是 HTTP URL 解码这样的东西http://stackoverflow.com/questions/13750290/how-to-decode-html-codes-using-java
    • 密码是1234%23,应该是1234#
    猜你喜欢
    • 1970-01-01
    • 2011-12-14
    • 2011-01-14
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多