Struts Nested Tag with Dynamic Parameters - java

I have a legacy Struts 1 application which uses the nested tag. Can I inject a dynamic parameter into the nested tag? For example,
<nested:select disabled="<c:out value='${requestScope.disableSelectBox}' />" />
I also tried doing:
<nested:select disabled="${requestScope.disableSelectBox}" />
In both of the above examples, the disabled attribute was not properly set and it was ignored. If I printout the value with a c:out, the correct value of disableSelectBox is displayed:
<c:out value="${requestScope.disableSelectBox}" />
A colleague suggested that I should use:
<nested:select disabled="<%=request.getAttribute("disableSelectBox"); %>" />
The trouble is that it is considered bad practice to use java scriplets in a JSP page. Is there any way to embed a dynamic variable into a Struts 1 nested tag? Switching to Struts 2 is not an option.
Thanks!

Struts 1 (as far as I can remember) cannot allow you to do:
<nested:select disabled="<c:out value='${requestScope.disableSelectBox}' />" />
As it can't process JSP tags inside any of their attribute declarations, Check what nested:select disabled attribute required needs.
But Struts do support EL and JSP Scriplets (so your colleague is correct). JSP Scriptlet will "render" the value of the <%=request.getAttribute("disableSelectBox"); %> and assign it to the <nested:select disabled="<%=request.getAttribute("disableSelectBox"); %>" />
So (if I assume that the values returns a true or false,
<nested:select disabled="${requestScope.disableSelectBox}" />
and
<nested:select disabled="<%=request.getAttribute("disableSelectBox"); %>" />
will be rendered as (if results returns true)
<nested:select disabled="true" />
before it is sent to Struts to render the nested tag (sorry for using the word "render", you can use translate if you want).

Related

Some syntax change in jsp from struts1 to struts2 --> logic:messagesPresent

I have below code. How to handle in struts2 of logic:messagesPresent which is for checking the property and display the <tld:label id="changepwd.error.info1" /> which is my jsp tag library and I think it can remain in struts2 as I have tested it can show the text.
<logic:messagesPresent property="error.message.missingNewPassword">
<tld:label id="changepwd.error.info1" />
</logic:messagesPresent>
logic:messagesPresent can be used only Struts 1.x.
You can use hasActionMessages() or hasActionErrors() instead of it, for example:
<s:if test="hasActionMessages()">
<tld:label id="changepwd.error.info1" />
</s:if>

The <fmt:setLocale value="${param.locale }" scope="session" />tag is not used

I use MyEclipse to compile my program,and I want to achieve internationalization, so I choose use the fmt tag.
The follow is the code:
<fmt:setLocale value="${param.locale }" scope="session" />
Book says that ${param.locale } can get the browser's default-language. In order to change the language, I use two languages, English and Chinese. Though I set the browser's default-language to English, when I reload the jsp page, the language is always Chinese. Could you tell me what's the matter?
full codes:
<%# taglib prefix="fmt" uri="java.sun.com/jsp/jstl/fmt"; %>
<fmt:setLocale value="${param.locale }" scope="session" /> <fmt:setBundlebasename="loginpage"/> <input type="text" id="text1" /> <br/> <input type="password" id="text2" /> <br /> <input type="submit" id="smb" value="<fmt:message key="login_sub" />" />
No that's not true. The EL param object maps a request parameter name to a single value. If param.locale exists then you can set locale through fmt:setLocale/> tag.
Text from this article - Formatting and internationalization through custom tags
The locale used by the JSTL tags when formatting data is normally
determined by examining the Accept-Language header sent by a user's
browser as part of each HTTP request. If no such header is present,
then JSTL provides a set of JSP configuration variables you can set to
specify a default locale. If these configuration variables have not
been set, then the JVM's default locale is used, which is obtained
from the operating system the JSP container is running on.
and take a look at SO thread - How to set JSTL locale from Java code?

How can I put a placeholder in a struts2 textfield tag?

I'm using struts2 tags and want to put a placeholder in a <s:textfield> tag like this:
<s:set name="email" value="getText('email')" />
...
<s:form action="Login">
<s:textfield key="email" theme="simple" placeholder="%{email}"
cssClass="span3"/>
...
</s:form>
email is defined in global.properties as "Correo electrónico" .
My problem is that when I see the jsp page, instead of seeing the value of email I see %{email}.
I read that it was a bug of Struts2 solved in version 2.3.1 here: https://issues.apache.org/jira/browse/WW-3644, but I'm using Struts2 2.3.4 and I keep having the same problem.
Anyone knows any solution to this problem or any other way for putting the placeholder in the textfield?
I had the same problem and I solved it like this:
<s:textfield name="user.email" placeholder="%{getText('settings.email')}" />
I needed up update both my Struts 2 and OGNL jars. My OGNL jar is ognl-3.0.5.jar.
You should be using the # prefix for variables created in the stack namespace but not pushed:
<s:textfield placeholder="%{#email}" ... etc ... />

Struts2 anchor tag doesn't include contextPath

%{#request.contextPath} doesn't work inside an s:a tag in Struts2. (Struts 2.2.1 to be specific.) Is there a way to make it work? It works in other Struts2 tags.
Here are two lines in a JSP file in a Struts 2 project whose context path is "/websites":
<s:a href="%{#request.contextPath}/clickme" theme="simple">Click here.</s:a>
<s:form method="post" action="%{#request.contextPath}/submitme" theme="simple"></s:form>
And here is the output:
Click here.
<form id="submitme" name="submitme" action="/websites/submitme" method="post"></form>
Notice that the context path is left off the anchor but is included in the form.
P.S. I can't use ${#pageContext.request.contextPath} here because ${} isn't allowed in Struts2 tags. Besides, I'm trying to be consistent. And I also try generally to avoid ${} since it does not auto-escape the output.
Thanks!
This should work:
<s:set id="contextPath" value="#request.get('javax.servlet.forward.context_path')" />
<s:a href="%{contextPath}/clickme" theme="simple">Click here.</s:a>
However, you're not supposed to do this. When you need an url, use the <s:url> tag:
<%-- Without specifying an action --%>
<s:url id="myUrl" value="clickme" />
<s:a href="%{myUrl}" theme="simple">Click here.</s:a>
<%-- With an action --%>
<s:url id="myUrl" action="clickme" />
<s:a href="%{myUrl}" theme="simple">Click here.</s:a>
By the way, you don't need a context path for the action attribute of a form:
<s:form method="post" action="submitme" theme="simple"></s:form>
Does Struts 2 support EL?
You can use ${request.contextPath} if it does....

Can Struts 1 in XHTML mode have 2 forms submitting to the same action?

We're converting an older Struts 1.x app from HTML4 to XHTML1.1. To force compliance from the Struts tags, we're adding
<html:xhtml />
to the top of our JSPs.
After doing this, JSPs using
<html:form method="post" action="/foo.do" styleId="bar">
threw the following error:
Cannot specify "styleId" when in XHTML mode as the HTML "id" attribute is already used to store the bean name
I read that The solution is to remove the styleId and use the name of your form bean, as that will be inserted as the id in your HTML. We can take out the styleId, but when 2 forms on the same page submit to the same action, they end up with the same id and the XHTML is no longer valid!
Do we have any other options while still using Struts tags?
If all you need to do is differentiate between the two forms, then just add a parameter to the action...
<html:form method="post" action="/foo.do?id=bar">
...
</html:form>
...
...
<html:form method="post" action="/foo.do?id=foo">
...
</html:form>
Then in your action, you just have to get the parameter from the request. Its been a while since I have used struts, but I had multiple forms going to the same action, and this is how I solved it.
1) For your xhtml compliance, rather do this:
<html:html xhtml="true">
</html:html>
The styleId="bar" is rendered to html as id="bar" and that's why you're getting an exception (because Structs generates the following in html)
<html:form method="post" action="/foo.do" styleId="bar">
to
<form id="foo" action"/foo.do" id="bar">
(Bear in mind that id="foo" depends on your declaration of <action name="foo" />). StyleId is used for element attribute purposes, e.g. styleClass="this" will render class="this"
A solution would be to add an id to the action, as in:
<html:form method="post" action="/foo.do?id=blah" styleId="bar">

Categories