I have a hidden field:
<input type="hidden" name="champs" id="champs">
I want do a <logic:equal> with the content of this field hidden, I tried the solutions but not work
<logic:equal name="virement" property="statut" value='champs' >
just be there is a syntax very precise in the value property of the logic: equal that I can found.
I'm a little rusty with this, but here are some hints:
Your field "champs" is a browser field, not a Java variable. When your request arrives at your servlet, the contents of browser fields have been transferred to request parameters. So the value in your field will end up in a predefined object called request, and for convenience broken down some more in a bean called param. There's a syntax for accessing this stuff... see below.
A little more detail (though I've seen it explained better) can be found in this page and maybe this one. Better yet, here
You can Google for "JSP Expression Language" to get more information.
You can use the struts tag html:hidden instead of input type = "hidden"
Related
I have a JSP attribute named "form" set followingly
<c:set value="${fieldAttributeMap[rowId].buildForm}" var="form" />
This works. The attribute "form" contains a Java object, which will used for further evaluation and displaying later in the JSP. However, I would like to use it in a more general way without knowing what the form name is beforehand - it could be named e.g. "modelForm" instead of "buildForm". If we assume that I have stored the name of the variable in JSP attribute "formName"
<c:set value="buildForm" var="formName" />
how can I use this to set the JSP attribute "form" like in the first code example? Basically the expression would have to be evaluated twice, like in this imaginary, non-working example:
<c:set value="${fieldAttributeMap[rowId][${formName}]}" var="form" />
Only workarounds which come to my mind are either writing my own tag or using the antiquated Struts bean:define tag. But I'm hoping there is some better solution or workaround.
Edit: there was a suggestion that this question may be a duplicate of calling another variable using a variable value as parameter in jstl However, the solution offered there is not applicable here, as I need to substitute the name of the attribute as a property of another Java object.
You simply need ${fieldAttributeMap[rowId][formName]}
I was explaining to a colleague the way of getting list data from a JSP page to back to the action class by using indices as explained here and here. He didn't quite understand and fumbled a bit on his own until he suddenly he made it work by not using indices at all!
In his JSP page he had:
<input type="checkbox" name="contactNameList" value="someValue1">
<input type="checkbox" name="contactNameList" value="someValue2">
<input type="checkbox" name="contactNameList" value="someValue3">
<input type="checkbox" name="contactNameList" value="someValue4">
In his action class he had the 'appropiate' setters:
public List<String> getContactNameList()
public void setContactNameList(List<String> list)
I'm baffled as to why this work. I think this works because he is sending non-bean data (in this case strings) and there is an intelligence build into Struts2/OGNL to append values to lists rather than overwrite them.
Can anybody explain with great detail what is going behind the hood in this "no indices" case? How is the list of strings instantiated and populated with the snippets above?
You should understand that bean data and not bean data are passed as parameters to the action. The parameters has a structure that you can find if you implement ParameterAware.
Note that all parameter values for a given name will be returned, so
the type of the objects in the map is java.lang.String[].
Then XWork Type Conversion make its best to convert this map to beans properties. See Built in Type Conversion Support.
Routine type conversion in the framework is transparent. Generally,
all you need to do is ensure that HTML inputs have names that can be
used in OGNL expressions. (HTML inputs are form elements and other
GET/POST parameters.)
In the no indexes case parameters are mapped under the one key, rather than indexed names are used under their own names.
You have to set index value in above code
like
<input type="checkbox" name="contactNameList[0]" value="someValue1">
I have a basic doubt in struts 1.x
Is there any difference in getting the value from jsp using request.getParameter('name') and form.get('name'), in terms of efficiency.
I know that form.get() returns an object and the former a string.
I want to know in the action class is it worth to get the dynaActionForm from the form argument of execute method and use it to get the user entered values in jsp. Or request.getParameter is enough ? Is there any other use of form object, if I typecast and create one ?
It's better to use request.getParameter().
I'm trying to do something that seems like it should be relatively straightforward and running into a bit of a wall.
Let's say I've got a list of products which I expose as a request attribute under the name products. Let's also say that each product has an id field, and that I also have a bunch of request attributes set in the form of selectedProduct_<product-id> to indicate which ones are selected.
I understand there are better ways to represent this information, such as placing all the selected ids into a Map and checking against that, but let's assume that I don't have access to that approach for whatever reason.
So what I'd like to do is iterate products and emit some markup only if there is a selectedProduct_... attribute set for the current product. Something like:
<c:forEach var="product" items="${products}">
<c:if test="${! empty selectedProduct_${product.id}}">
<div class="productId">${product.id}</div>
</c:if>
</c:forEach>
But of course that doesn't work, as it dies on ${! empty selectedProduct_${product.id}}.
What will work is if I hardcode the product-id into the expression, like:
${! empty selectedProduct_17}
...assuming that '17' is a valid product id. Obviously that's not practical, but hopefully it illustrates what I'm trying to accomplish. Basically I need to:
Determine the correct selectedProduct_... value to use for each iteration in the forEach loop. Something as simple as <c:set var="key" value="selectedProduct_${product.id}"/> would do this, except I'm not sure how to then take key and use it to get the value of the request attribute with that name (without cheating and running literal Java code inside a <% %> block).
Get the value of the request attribute whose name I determined in #1. This seems to be the tricky part.
Is this possible using pure JSP/JSTL? I know I could run some Java code inside of <% %> to solve this, but that seems like it would be in exceedingly bad form. Surely a more elegant solution exists?
You can by using implicit objects:
There are objects that allow access to the various scoped variables described in Using Scope Objects.
pageScope: Maps page-scoped variable names to their values
requestScope: Maps request-scoped variable names to their values
sessionScope: Maps session-scoped variable names to their values
applicationScope: Maps application-scoped variable names to their values
When an expression references one of these objects by name, the appropriate object is returned instead of the corresponding attribute. For example, ${pageContext} returns the PageContext object, even if there is an existing pageContext attribute containing some other value.
So, for example:
<c:set var="selectedProductAttrName" value="selectedProduct_${product.id}"/>
${requestScope[selectedProductAttrName]}
I'm trying to make a set of custom tags that encapsulate form elements (markup and validation).
There's a method given to retrieve the "Out" object easily:
JspWriter out = getJspContext().getOut();
However I can't figure out how to get the request object. I want to be able to directly access the submitted form values from within the Tag class so that I can validate each field.
The documentation is quite sparse, so I thought maybe I could use the JspContext object to somehow get the request attributes. But I don't understand the different scopes.
System.out.println(getJspContext().findAttribute("field1"));
always prints "null".
Enumeration e = getJspContext().getAttributeNamesInScope(1);
Looping through and printing out the enumeration just gives me a list of classes that don't exist:
javax.servlet.jsp.jspOut
javax.servlet.jsp.jspPage
javax.servlet.jsp.jspSession
javax.servlet.jsp.jspApplication
javax.servlet.jsp.jspPageContext
javax.servlet.jsp.jspConfig
javax.servlet.jsp.jspResponse
javax.servlet.jsp.jspRequest
So is this even possible?
If not, could anyone point me to a tag library that deals with form display and validation? I searched the internet for a couple hours and it seemed every single one was discontinued and I couldn't download them. Either that or suggest a better alternative for handling forms.
Edit: The tags extend the SimpleTagSupport class.
If your class is extending TagSupport, you can access the protected pageContext variable. From that you're able to retrieve the request object.
http://java.sun.com/webservices/docs/1.5/api/javax/servlet/jsp/tagext/TagSupport.html#pageContext