复选框值“on”未提交给操作 class。我应该直接从 HttpServletRequest 获取参数吗?
Checkbox value `on` is not submitted to Action class. Shall I get parameter from HttpServletRequest directly?
我想通过 Struts 2 classic getter/setter
方式向我的操作 class 提交表单。所有其他字段都获得了它们的值,除了 boolean
字段,当我选中它对应的复选框时它保持 false
。
我的 jsp:
中有一个这样的复选框
<div class="col-md-3">
<input class="input-md" type="checkbox" id="soloIdaId" name="soloIda" />
</div>
在我的操作中 class 我有:
private boolean soloIda;
及其 getter/setter。但是我查了一下还是假的
我注意到 queryString
中包含:
...&soloIda=on&... (I am testing with FF)
而且,如果我尝试获取请求的参数,我会得到 true
:
HttpServletRequest request = ServletActionContext.getRequest();
String soloIdaParam = request.getParameter("soloIda"); //"true" when I debug
不知道这是否重要,但我正在使用我的拦截器。但我总是包含 defaultStack
,我相信默认拦截器正在处理这种工作(使用 getter/setter 为字段赋值)。那为什么?
在这里附上我的 struts.xml
:
<interceptors>
<interceptor name="authentication"
class="com.WindThunder.interceptor.LoginInterceptor">
</interceptor>
<interceptor name="query"
class="com.WindThunder.interceptor.QueryInterceptor">
</interceptor>
<interceptor-stack name="authStack">
<interceptor-ref name="authentication"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref> <!-- this
line is necessary for every stack!!!! -->
</interceptor-stack>
<interceptor-stack name="pageStack">
<interceptor-ref name="query"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="searchCiudad" class="com.WindThunder.action.VueloAction"
method="searchCiudad">
<interceptor-ref name="pageStack"/>
<result name="success">/listaCiudads.jsp</result>
<result name="error">/common/error.jsp</result>
<result name="vacio">/common/listaVacia.jsp</result>
</action>
还有我的 QueryInterceptor.java
:
@Override
public String intercept(ActionInvocation invocation) throws Exception {
//get previous action name = pagePath. (/searchxxx.action)
this.session = invocation.getInvocationContext().getSession();
HttpServletRequest request = ServletActionContext.getRequest();
String path = request.getRequestURI(); //contaits domain
String pagePath = path.substring(path.lastIndexOf("/"));
//get previous query string (param1=xx¶m2=xx, or null)
String queryString = request.getQueryString();
addParamToSession(queryString, session);
String prePage = "";
if (!pagePath.equalsIgnoreCase("/login.action")){
prePage = pagePath;
}
if (prePage != null && !prePage.equals("")){
session.put("prePage",prePage);
}
return invocation.invoke();
}
现在我想直接从request
获取参数"soloIda"
并做一些手动赋值,但我认为这是Struts1的工作方式。知道为什么它不起作用吗?我一定是做错了什么。
这不是你的拦截器的错...只是复选框是一个丑陋的东西。
要知道的事情:
- 未指定值时浏览器为复选框发送的内容是特定于浏览器的;
- 浏览器不会为未经检查的值发送任何内容(这在布尔值的情况下可能无关紧要,因为默认值为 false,但仍然...)
然后你可以用
强制值
<input class = "input-md"
type = "checkbox"
id = "soloIdaId"
name = "soloIda"
value = "true" />
然后您需要处理未处理的值。
为了处理这种情况,Struts2 为您提供了 <s:checkbox />
(and the <s:checkboxlist />
) tag, with its value
and fieldValue
attributes, and the hidden parameter (__checkbox_something
) that the tag generates to send something even when the checkbox is not checked. This parameter is then read by the Checkbox Interceptor。
例如。来自文档:
<s:checkbox cssClass = "input-md"
id = "soloIdaId"
name = "soloIda"
fieldValue = "true"
value = "aBoolean"
/>
将转换为以下内容 HTML(具有简单主题和 Boolean aBoolean = true;
):
<input type = "checkbox"
class = "input-md"
id = "soloIdaId"
name = "soloIda"
value = "true"
checked = "checked"
/>
<input type = "hidden"
id = "__checkbox_soloIdaId"
value = "true"
name = "__checkbox_soloIda"
/>
阅读 Mkyong example on <s:checkbox />
and also this SO answer 以更好地了解从哪里开始。
始终考虑使用框架特定的标签(至少一开始是这样),它已经为您透明地处理了很多问题。
我想通过 Struts 2 classic getter/setter
方式向我的操作 class 提交表单。所有其他字段都获得了它们的值,除了 boolean
字段,当我选中它对应的复选框时它保持 false
。
我的 jsp:
中有一个这样的复选框<div class="col-md-3">
<input class="input-md" type="checkbox" id="soloIdaId" name="soloIda" />
</div>
在我的操作中 class 我有:
private boolean soloIda;
及其 getter/setter。但是我查了一下还是假的
我注意到 queryString
中包含:
...&soloIda=on&... (I am testing with FF)
而且,如果我尝试获取请求的参数,我会得到 true
:
HttpServletRequest request = ServletActionContext.getRequest();
String soloIdaParam = request.getParameter("soloIda"); //"true" when I debug
不知道这是否重要,但我正在使用我的拦截器。但我总是包含 defaultStack
,我相信默认拦截器正在处理这种工作(使用 getter/setter 为字段赋值)。那为什么?
在这里附上我的 struts.xml
:
<interceptors>
<interceptor name="authentication"
class="com.WindThunder.interceptor.LoginInterceptor">
</interceptor>
<interceptor name="query"
class="com.WindThunder.interceptor.QueryInterceptor">
</interceptor>
<interceptor-stack name="authStack">
<interceptor-ref name="authentication"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref> <!-- this
line is necessary for every stack!!!! -->
</interceptor-stack>
<interceptor-stack name="pageStack">
<interceptor-ref name="query"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="searchCiudad" class="com.WindThunder.action.VueloAction"
method="searchCiudad">
<interceptor-ref name="pageStack"/>
<result name="success">/listaCiudads.jsp</result>
<result name="error">/common/error.jsp</result>
<result name="vacio">/common/listaVacia.jsp</result>
</action>
还有我的 QueryInterceptor.java
:
@Override
public String intercept(ActionInvocation invocation) throws Exception {
//get previous action name = pagePath. (/searchxxx.action)
this.session = invocation.getInvocationContext().getSession();
HttpServletRequest request = ServletActionContext.getRequest();
String path = request.getRequestURI(); //contaits domain
String pagePath = path.substring(path.lastIndexOf("/"));
//get previous query string (param1=xx¶m2=xx, or null)
String queryString = request.getQueryString();
addParamToSession(queryString, session);
String prePage = "";
if (!pagePath.equalsIgnoreCase("/login.action")){
prePage = pagePath;
}
if (prePage != null && !prePage.equals("")){
session.put("prePage",prePage);
}
return invocation.invoke();
}
现在我想直接从request
获取参数"soloIda"
并做一些手动赋值,但我认为这是Struts1的工作方式。知道为什么它不起作用吗?我一定是做错了什么。
这不是你的拦截器的错...只是复选框是一个丑陋的东西。
要知道的事情:
- 未指定值时浏览器为复选框发送的内容是特定于浏览器的;
- 浏览器不会为未经检查的值发送任何内容(这在布尔值的情况下可能无关紧要,因为默认值为 false,但仍然...)
然后你可以用
强制值<input class = "input-md"
type = "checkbox"
id = "soloIdaId"
name = "soloIda"
value = "true" />
然后您需要处理未处理的值。
为了处理这种情况,Struts2 为您提供了 <s:checkbox />
(and the <s:checkboxlist />
) tag, with its value
and fieldValue
attributes, and the hidden parameter (__checkbox_something
) that the tag generates to send something even when the checkbox is not checked. This parameter is then read by the Checkbox Interceptor。
例如。来自文档:
<s:checkbox cssClass = "input-md"
id = "soloIdaId"
name = "soloIda"
fieldValue = "true"
value = "aBoolean"
/>
将转换为以下内容 HTML(具有简单主题和 Boolean aBoolean = true;
):
<input type = "checkbox"
class = "input-md"
id = "soloIdaId"
name = "soloIda"
value = "true"
checked = "checked"
/>
<input type = "hidden"
id = "__checkbox_soloIdaId"
value = "true"
name = "__checkbox_soloIda"
/>
阅读 Mkyong example on <s:checkbox />
and also this SO answer 以更好地了解从哪里开始。
始终考虑使用框架特定的标签(至少一开始是这样),它已经为您透明地处理了很多问题。