I want to extend Struts 2 s:select tag.
Can somebody explain step required to do the same.
Also if there is any hook or implementation available in Struts2 framework for the same
I think you can change the freemarker template as you need and extend org.apache.struts2.views.jsp.ui.SelectTag class.
Check this article to start with.
You can start by examining the freemarker templates (default is freemarker but it can be velocity or some other template engine). See http://mikeski.net/site/node/16.
If that is not enough for you then you could extend or create your own implementation of select tag. http://joshuajava.wordpress.com/2008/12/27/creating-custom-components-with-struts-2/
Related
How can I customize the template of my field plugin? how to apply bootstrap classes to it?
Where can I get the default template of a f:field (for example) to customize it ?
Thank you
You can check documentation of grails field plugin, I think is very clear Grails Field Plugin Custimization Page
In resume, you have to create gsp file templates, for the diferent fields, it can be done by varios options for example property type, propery name, controller, action, etc. In those template files you can apply the bootstrap clases you want to use.
For passing XSRF token with Struts2 forms, I have to put the token tag inside all forms. The baseline jsp in tiles-def can't have an all-encompassing form.
Have you ever extended the form tag to include token tag by default or know of some library that does that?
I haven't explored Freemarker template, so do not know if this is feasible or not. If there are no existing solution, I'll try to build my own.
To consolidate from the comments section,
1) Create a new theme
2) Extend "form-close.ftl" to this
<#s.token/>
<#include "/${parameters.templateDir}/xhtml/form-close.ftl" />
Add tokenSession (or token) interceptor in your stack.
With these changes, all struts forms will have a struts-token added without specifying <s:token> in each of them.
I am building out a dynamic table using JSTL and was hoping to make use of the c:out tag to help build out some expressions, but am not able to find that tag available among the other JSTL core tags.
I have the following namespace being used:
xmlns:c="http://java.sun.com/jsp/jstl/core"
and made sure my web.xml file was set to use the 2.5 spec found here
https://stackoverflow.com/tags/jstl/info
but still only find catch, choose, forEach, if, otherwise, set, and when.
Additionally, I tried importing the JSTL 1.2.1.jar libraries as well with no success.
So, should the c:out tag be available for me to use in JSF2 ? If so, what steps am I missing?
Regards,
Mike
The <c:out> indeed not available in JSF2 Facelets. You don't need it in JSF2 Facelets anyway. Just use the JSF equivalent <h:outputText>,
<h:outputText value="#{bean.text}" />
or even better, just put EL in template text,
#{bean.text}
It will already be implicitly XML-escaped if that's your sole concern (and was during old JSP2 ages actually the sole reason why <c:out> is been used; in JSP1 the tag was simply mandatory in order to display a bean property as EL in template text wasn't supported in JSP1). The <c:out> offers no additional advantages over those standard JSF2 Facelets ways, that's why it's been removed from the JSTL subset for Facelets.
See also:
Is it suggested to use h:outputText for everything?
Is there a way to dynamically add an attribute to a struts 2, tag UI tag such as a textfield?
The reason is that I want to add a readOnly form field attribute to an <s:textfield/>, depending on an action's method result. I cannot use readOnly="%{isReadOnly()}" since once the attribute is defined, the form element is read-only, no matter what value it has. And wrapping each form field into an <s:if/> tag is pretty cumbersome and results in a lot of code duplication.
I would also like to avoid JavaScript for interoperability reasons and for not relying on the browser's scripting settings.
If the issue is to use the built in struts2 functionality then one easy option is to render your view with freemarker, which readily supports the dynamic addition of attributes.
If you are using conventions, it is VERY trivial you just need to create a file with a ".ftl" extension, if you are using xml it is also very easy just use the freemarker result type (see here for greater description):
<action name="test" class="package.Test">
<result name="success" type="freemarker">/WEB-INF/content/testView.ftl</result>
</action>
Here is example view using a map to dynamically add attributes (example also taken from liked page):
<#s.textfield name="test" dynamicAttributes={"placeholder":"input","foo":"bar"}/>
The dynamicAttributes would be extremely useful in all JSP UI tags but alas it is not currently implemented.
NOTE: There is one error/omission in the above link. It tells you to add the following line which causes an error in my environment (simply the line is not needed).
<#assign s=JspTaglibs["/WEB-INF/struts.tld"] />
That is, this line in a file all by it self is sufficient for rendering a text element, no explicit tag library declaration needed!
<#s.textfield name="test" dynamicAttributes={"placeholder":"input","foo":"bar"}/>
There are a number of advantages to using freemarker over plain JSPs, so taking a moment to explore the syntax and using it for this one case may prove useful later.
Is it mandatory in struts2 to use struts-tags. I mean can't we use following:
<td bgcolor="DEDBC6"><div align="left"><font color="#000000"><strong>
Employee Name * :</strong></font></div></td>
<td><input name="empname" type="text" id="empname" size="32"></td>
instead of following:
<s:textfield label="Employee Salary " name="empsal" required="true"/>
I had tried both but when i used 1st i didn't get validation message i.e "empname is required" that i wrote in action-validation.xml although validation is working well.
please comment ?
No, it is not mandatory. In fact a lot of struts2 users are sufficiently dissatisfied with OGNL and choose to use regular HTML instead.
But using the standard HTML tags has the drawback of loosing some functionality, after all that's why the custom tags are there in the first place. It is possible to get validation through validate methods on the controller class even with standard HTML.
If you are just starting out with the framework I suggest you learn the struts tags properly before going off the beaten path.
Is it mandatory in struts2 to use struts-tags.
No, but if you aren't using tags at all then you're not really getting very much out of Struts as a framework. Might as well do it yourself.
i didn't get validation message
If you're using your own markup you'll have to tell it to display the error message. eg.:
<s:fielderror><s:param>empcode</s:param></s:fielderror>
please comment ?
Please stop asking the same questions over and over again.
I don't see why you need any tags at all. Couldn't you just use Struts to generate JSON result types and then use calls to those in your otherwise static jsp pages using JavaScript and JQuery?