@FormParam 和 ${param} 之间的冲突

Conflict between @FormParam and ${param}

在我看来,我可以使用表达式语言打印来自当前请求的 POST 表单值:

<p>Foo Name: ${fn:escapeXml(param.fooName)}</p>

但是当我在我的控制器中使用 @FormParam 注释将表单数据捕获到 Java 变量时,param.fooName EL 变量不再被填充:

@FormParam("fooName") String fooName

完整流程如下:

  1. 浏览器将表单提交至 http://localhost:7101/myapp/rs/foo/new:

    @POST
    @Path("/new")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.TEXT_HTML)
    public Response saveNew(
        @FormParam("fooName") String fooName // <---
    ) {
        Map<String, Object> map = new HashMap<String, Object>();
        // Do stuff here
        return Response.ok(new Viewable("/foo/new", map)).build();
    }
    
  2. 视图从 new.jsp:

    渲染
    <%@ page contentType="text/html" pageEncoding="UTF-8" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    <p>Foo Name: ${fn:escapeXml(param.fooName)}</p>
    

如果我删除 @FormParam("fooName") String fooName 行,那么 ${fn:escapeXml(param.fooName)} 将再次包含表单数据。

有没有办法在两个位置(Java 控制器和 JSP 视图)访问表单数据?

错误或功能,@FormParam 似乎有副作用。但是,表单数据在 javax.servlet.http.HttpServletRequest 处可用,因此:

@POST
@Path("/new")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_HTML)
public Response saveNew() {
    // null when it doesn't exist (no exception thrown)
    String fooName = request.getParameter("fooName");

    Map<String, Object> map = new HashMap<String, Object>();
    // Do stuff here
    return Response.ok(new Viewable("/foo/new", map)).build();
}
<%@ page contentType="text/html" pageEncoding="UTF-8" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<p>Foo Name: ${fn:escapeXml(param.fooName)}</p>