Suppress empty parameters using param tag(使用 param 标签抑制空参数)
问题描述
我编写了用于许多操作的 JSP.它有一个参数链接
I have written JSP that I used with many actions. It has a link with parameters
链接:
<s:a namespace="/some" action="view">
  <s:param name="purpose" value="%{purpose}"/>
  <s:param name="type" value="%{type}"/>
  <s:property value="%{name}"/>
</s:a>
动作类:
public class ViewAction extends ActionSupport {
  private Long purpose;
  private Long type;
  private String name;
  public Long getPurpose() {
    return purpose;
  }
  public void setPurpose(Long purpose) {
    this.purpose = purpose;
  }
  public Long getType() {
    return type;
  }
  public void setType(Long type) {
    this.type = type;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
}
通常我会初始化两个参数,但有时一个参数是null.所以,链接是用 href 生成的,比如
Usually I initialize both parameters, but sometimes one parameter is null. So, the link is generated with href like
/context/some/view?purpose=1&type=
但我想删除 &type= 
我尝试了 Param 示例.
<s:a namespace="/some" action="view">
  <s:param name="purpose" value="%{purpose}"/>
  <s:param name="type" value="%{type}"/>
  <s:property value="%{name}"/>
  <s:param name="suppressEmptyParameters" value="true"/>
</s:a>
但是没用
我也试过了
<s:a namespace="/some" action="view">
  <s:param name="purpose" value="%{purpose}"/>
  <s:param name="type" value="%{type}" suppressEmptyParameters="true"/>
  <s:property value="%{name}"/>
</s:a>
我得到了
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
    at org.apache.struts2.components.Param.end(Param.java:129)
    at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
如何解决这个问题?
推荐答案
那行 <s:param name="suppressEmptyParameters" value="true"/> 没有意义,并且应该从 <s:param> 文档中删除.
Well that line <s:param name="suppressEmptyParameters" value="true"/> doesn't make sense, and it should be removed from the <s:param> docs.
<s:param name="type" value="%{type}" suppressEmptyParameters="true"/> 是抑制空参数的正确方法,但它不起作用由于错误 WW-4275 而不是 String-s.
The <s:param name="type" value="%{type}" suppressEmptyParameters="true"/> is correct way of suppressing empty parameters and it isn't working with not String-s because of the bug WW-4275.
同时,在下一个版本发布之前,您可以使用toString()方法来避免ClassCastException异常.
Meanwhile, until the next version is released, you can use toString() method to avoid ClassCastException exception.
<s:param name="type" value="type.toString()" suppressEmptyParameters="true"/>
这篇关于使用 param 标签抑制空参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 param 标签抑制空参数
 
				
         
 
            
        - Java包名称中单词分隔符的约定是什么? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
 
						 
						 
						 
						 
						