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>
Related
I want equivalent Struts 1.3 tag for <s:property value="Content" escapeHtml="false" />
Currently I am trying something like <bean:write name="MyForm" property="Content" filter="false" ></bean:write>
I want to hide/remove HTML tags from My Content - I am having data from CKEditor which is combination of HTML tags.
Using bean:write tag you can output the value from the action form property. If you want to use a scope variable, you can use JSP EL or JSTL c:out
${MyForm.Content} or
<c:out value="${MyForm.Content}" escapeXml="false"/>
This helped me for removing escape characters from CkEditor get Data function Bean:write
(Oups.. Sorry for my english :) )
In my web application, Struts2 is used as the main Servlet dispatcher and filter. But For some reasons, i have a custom filter and a custom servlet used for a specific url "/book".
But I have some commons jsp... i had some issues when the custom Servlet should display my request attributes in the JSP because of the struts tags (implemented before). So i changed these tag by the jstl taglibs and it works now.
But... In one JSP, the main (lol)... I have a search form.. This JSP is included in several JSPs and could be called by Struts and the custom Servlet..
With only Struts the tag was "< s:form>.." and when the form was submitted, all sended values was kept in the input... But now, because of the custom Servlet i use a simple html form which is calling the struts action "search.do".
As source code is below:
<form method="post" action="<c:out value="${contextPath}"/>/search.do" name="search" id="search">
<input type="text" id="search_searchWord" value="" maxlength="200" size="100" name="searchWord">
<div align="right">
<input type="submit" value="Ok" name="searchButton" id="search">
</div>
<select id="search_searchCrit" name="searchCrit">
<option value="0">Crit1</option>
<option value="1">Crit2</option>
<option value="2">Crit3</option>
</select>
</form>
My problem is the search word and the selected option are refreshed after the submit. I need to keep them !
Is there a way to use the struts taglibs with a Standard Servlet ?
Or Do you have another solution to keep the submitted information ?
Thanks all !
take each field value from the input field and write js function to fill each field in jsp source code of your page.
function selectedValue(){
var value =<%=request.getParameter("searchCrit")%>;
if(value !=null)
{
document.getElementById('search_searchCrit').innerHTML=value;
}
}
I found a solution with the help of #java_seeker.
In my Struts action, i got the request through this way :
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("searchWord", this.getSearchWord());
There is two different way to do this, see: http://www.mkyong.com/struts2/how-to-get-the-httpservletrequest-in-struts-2/
The attribute is setted in each method (in the action) that could refresh the page.
Then, i just recovered and set the attribute from the request as a variable with a jstl tag and display it as the value of my html input:
<c:set var="searchWord" value='<%=request.getAttribute("searchWord") %>' />
<input type="text" id="search_searchWor" value='<c:out value="${searchWord}" />' name="searchWord">
For the , i just used an <c:choose><c:when test=""></c:when><c:otherwise><c:otherwise></c:choose> to set the selected choice.
Now all value are always displayed. Maybe it's not the very good way to display share the same JSP between a standard servlet and a Struts action, but it works. I'm open to try a better solution if you have one! Thanks all!
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 ... />
%{#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....
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).