Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

java - Suppress empty parameters using param tag

I have written JSP that I used with many actions. It has a link with parameters

The link:

<s:a namespace="/some" action="view">
  <s:param name="purpose" value="%{purpose}"/>
  <s:param name="type" value="%{type}"/>
  <s:property value="%{name}"/>
</s:a>

The action class:

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;
  }
}

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=

but I want to remove &type=

I tried an example from Param Examples.

<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>

but it didn't work

I also tried

<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>

and I got

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)

How to solve this problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well that line <s:param name="suppressEmptyParameters" value="true"/> doesn't make sense, and it should be removed from the <s:param> docs.

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.

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"/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...