Domanda

I'm developing a web application using JSP/Servlet/JSTL in Eclipse Juno and GlassFish 3.1.2.1. What I need to achieve is to find a way to set the empty strings as null, E.g. Receive a parameter from another page that is set to an empty string and detect it as null. I have been searching and I found a parameter in JSF that I put in the web.xml .

<context-param>
    <param-name>
        javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL
    </param-name>
    <param-value>true</param-value>
</context-param>   

But It didn't work. So what am I missing in those lines? How can I set the empty strings as null?.

As an extra note I need to set this because the system will be working in a Websphere where the empty strings are treated as null.

Thanks in advance.

UPDATE

Another example could be like this:

OnePage.jsp

<form action="anotherPage.jsp">
   <input type="text" name="foo" id="idFoo" value="" />
</form>

anotherPage.jsp

<c:out value="${param.foo}"/>

In GlassFish 3.1.2.1 the c:out tag would print nothing as the value is set to empty string. In Websphere Application Server, the value would be null. I need that GlassFish display null values instead of empty strings, as Websphere does.

È stato utile?

Soluzione

You are misunderstanding about how JSTL expression work. If the variable is NULL OR event Not exists c:out will produce NOTHING (Empty). This is a specification of JSTL expression. I don't understand why your WebSphere produce "null". You need to re-check.

For examples:

  1. if foo == null --> Result is empty
  2. if foo does not exists --> Result is empty ( No exception )
  3. if foo is empty --> Of course the result is empty
  4. if foo is "null" ( String ) ==> the result is null

For your case. If you want to display NULL instead of empty, you need to do like this:

<c:if test="${empty param.foo}">null
</c:if>
<c:if test="${not empty param.foo}">
    <c:out value="${param.foo}"/>
</c:if>

OR you can create JSTL function to handle this case:

<tf:outNullOrValue (param.foo) />

The function outNullOrValue like this:

public static String outNullOrValue(String s) {
    return s == null ? "null" : s;
}

Of course, you need to create TLD for the function and declare taglib directive in your JSP.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top