In one of our challenging application, I am generating few struts tag dynamically in Action class
Once I generated the struts tag, I want it to get it parsed through s:property
in my JSP:
<s:property value='generateElement("ABC")' escape='false' />
in my Java Action
public String generateElement(String element){
return "<s:select id='aaaa' list=\"{'1':'1','2':'2'}\" >";
}
in the end I want to generate Selection box.
How do I achieve this.
You can't execute JSP code after the JSP has been compiled. So there's no use in returning a JSP tag in your method. Instead, generateElement should return the objects that you need in your select, and then reference that property in a <s:select> tag that is already in your JSP.
Related
I am working on the Struts2 framework with JSP.
In my samplePrj.properties file, in that
com.samplePrj.Successmessage = Saved Successful
is an attribute. I need to use this value in my JSP page, using Struts2.
so how can I get the value of "com.samplePrj.Successmessage" in my JSP page.
Use the text tag
<s:i18n name="samplePrj">
<s:text name="com.samplePrj.Successmessage" />
</s:i18n>
it will load the bundle using i18n tag from samplePrj.properties and print the value from key com.samplePrj.Successmessage in it.
or you can use it with getText() but your action class should extend ActionSupport.
<s:property value="getText('com.samplePrj.Successmessage')"/>
You can use getText() method to read from properties files.
<s:set var="variable" value="getText('com.samplePrj.Successmessage')"/>
<s:if test="myVariable == #variable">
//do what u want
</s:if>
I'm trying to use Struts framework in my project. I want to use the html:button to send parameter in the link but I don't understand how to make it.
In other words, I want to translate this line:
<input type="button" onClick="window.location.href='resum.do?action=ViewMessage&&id_message=<%= id_msg %>'" value="View"/>
to Struts taglib, something like this:
<html:button property="" onclick="window.location.href='resum.do?action=ViewMessage&&id_message=<%= id_msg %>'" value="View"></html:button>
But it didn't work.
The html:button tag is used only inside the form tag. See the docs
This tag is only valid when nested inside a form tag body.
Also set the property attribute.
I have a custom tag that has no body at all. I'm trying to programmatically replace the empty body with, for simplicity's sake,
[<c:out value="SUCCESS!"/>]
The goal is to see "[SUCCESS!]" displayed by the JSP which uses the tag, but all I see is "[]" and if I look at the generated source code, I can see that the c:out statement is written on the page between the brackets, but not interpreted.
Is there a common way to achieve this ? The final goal will be to use other custom tags instead of the "c:out" tag. The tags/content will come from a database.
I tried different techniques with SimpleTagSupport and BodyTagSupport but none of those were successfull. In fact I'm not sure if it is technically possible to do it, since, the tag has already been interpreted at that time.. But then how should this be done ?
Server tags (like your custom tag or JSTL tags) get transformed to Java code when the JSP is translated into a servlet. For example, the following JSP code:
<c:out value="FooBar" />
gets translated to something like this inside the servlet:
....
OutTag outTag = (OutTag) tagHandlerPool.get(OutTag.class);
outTag.setPageContext(pageContext);
outTag.setParent(null);
outTag.setValue(new String("FooBar"));
int evalOut = outTag.doStartTag();
....
In your custom tags you can call other Java classes/methods and can write HTML code (not JSP code) to the response.
The [<c:out value="SUCCESS!"/>] is not interpreted because at this level it's just a string that gets written directly to the response.
I am trying to read data from a table and display it to the user .
Can anybody tell how to do it using struts 1.3 ?
Write an class extending Struts' Action class. This class pulls the data from Database as a List. Pass this data as request attribute, request.setAttribute("myList", list). Return "success".
In your struts-config.xml, map this Action class to a JSP on "success". The request will be forwarded to the JSP.
In the JSP, get the list from request by request.getAttribute("myList"). Iterate through the list and print the List.
You need to study this: http://struts.apache.org/1.x/userGuide/index.html
(Edit: Just noticed this is a 2 year old question)
Don't use the struts tags unless you NEED to. This can be accomplished with jstl/el.
So on your Action class you would have something like this:
List<Map<?, ?>> listOfHashMaps = new ArrayList<Map<?, ?>>();
request.setAttribute("listOfHashMaps", listOfHashMaps);
In your jsp:
<c:forEach var="hashMap" items="listOfHashMaps">
${hashMap[someInteger]} <%-- To get the value associated with 'key' --%>
</c:forEach>
You can also access the keys/values with:
${hashMap.key}
${hashMap.value}
Respectively.
i am using diaply tags library for displaying my tables.But i cant use the <% %>
tagst there.If i try to us it it gives me error.but i can use tag there.
If i try to use followin code in my jsp it give an error sayin shoul hav a matcheing ending tag.
i have follown java code in jsp
List<Course> = (List<Course>)request.getAattribute("crc");
here Course is a class/bean.
can anyone suggest me such library that i can use with struts for auto paging,displaying list in tables,and with other features provided by display tag.I want to use struts and i want the view to look good and yet easy to devlop.that is i want to achieve high class userinterface with littel effortr toward displaying o/p / view.
can anyone provide the example of disploay tag with struts
You can use the name attribute of the table tag (normally like this:)
<display:table name="crc" ...>
</display:table>
To use the crc List as the basis of Javabeans to display.
See http://displaytag.sourceforge.net/1.2/displaytag/tagreference.html#display-el:table for more information about the table tag.