I'm trying to add an action to a link inside a list item
It's inside a form:
<form name="searchForm" method="post" action="searchThis" id="querySubmiter">
Inside a list:
<li><img src="img/Logo/myLogo.png"/>
I'm a bit clueless as to how to make the connection.
Is it possible?
Use Struts 2 tags like
<s:a action="searchThis"><img src="<s:url value="/img/Logo/myLogo.png"/>"/></s:a>
There would be generated an anchor tag with the image. Note to use <s:url> tag to correctly define the path to the static resource. Also combined with <s:param> tag you could construct any link. See the documentation with example of the <s:a> tag.
Related
<a href="printAllocation.action?event=printAllocation&fundAlias=%{fundAlias}&fundName=%{fundName}&lendFromMonth=%{lendFromMonth}&lendFromYear=%{lendFromYear}&partListDate=%{partListDate}&partListType=%{partListType}&secLendIncome=%{secLendIncome}"
onClick="NewWindow(this.href,'Warning','1000','800','Yes');return false;">
<s:submit value="Print" cssClass="btnRedApple">
</s:submit></a>
Hi! in the above code I'm trying to pass the values from the current jsp to new window by using anchor tag for the button "print" but in new window I'm not getting the values.
This is done for the struts2 migration so can any one please help me in resolving this issue. how can I pass the value to the new window
Looks like you have a URL with many parameters. What makes it worse is including the URL in anchor tag. I suggest you use s:url of stuts 2.
Struts 2 “url” tag is used to create an URL and output it as a text
format. It’s never work by itself, but it can provides URL to other
tags like to create a hyperlink or to render an image
<s:url action="printAllocation.action" var="urlTagValue" >
<s:param name="event">printAllocation</s:param>
<s:param name="fundAlias" value="%{fundAlias}"></s:param>
<s:param name="fundName" value="%{fundName}"></s:param>
...//all the other parameters
</s:url>
this essentailly generates output in text format like below.
/theDomainOfYourWebApp/printAllocation.action?event=printAllocation&fundAlias=...
Add then use it your anchor tag
<a href="<s:property value="#urlTagValue" />" onClick="NewWindow(this.href,'Warning','1000','800','Yes');return false;>URL Tag Action (via property)</a>
I am working on a Java (Struts) based web application. I have to show some content by getting from DB in jsp. Example content:
<div>
some code here
<a> link here</>
</div>
I am using textarea for the time but when I put this into textarea using following tag:
<html:textarea property = "contentFromDB" rows = "12" cols = "70" styleClass = "textarea" disabled="true"/>
It shows the HTML in the textarea, is there any way I may show the content in the textarea with out HTML tags or I may show the content in any other tag like P or span, I cannot find these tags in the struts tagLib as well.
The <html:xxx> tags are for forms.
The best practice is to use JSTL's <c:out> with an appropriately-scoped object (e.g., a request attribute) and set escape to false:
<c:out value="xxx" escapeXml="false" />
Alternatively if you're on an antiquated container or just have nothing better to do, use the <bean:write> tag. I don't recall its escaping behavior(s), if any.
I am currently using Struts2 tags for my form, and to show its error messages. My question is that the default markup for showing error messages in Struts2 tags is the usage of <ul> tag. is there anyway I can change this? I want the error messages to be displayed as <span> not a list.
How would I achieve this?
Another option is to change the CSS for UL elements.
This approach works only if you specifically care about appearance, not the DOM itself.
You can override template files which are used for rendering errors. Copy actionerror.ftl and fielderror.ftl files from the simple theme from struts2-core jar to your application and modify them not to use ul/li tags.
The tags render according to their theme. The question then changes to: How do you change the theme? You can change it for the tag
(set theme attribute on the tag to simple), page, request, or generally.
http://struts.apache.org/2.2.1/docs/struts-2-themes.html
Personally I like writing html, that is I don't like any "help" from the struts2 default theme. So in my struts.xml I simply use:
<constant name="struts.ui.theme" value="simple" />
Web developers should know html.
Update:
Generally use YUI reset.css so I probably missed this...
If you extend ActionSupport on the action there is a getFieldErrors() method so you could use <s:property value='fieldError["field_name"]'/> that will return the associated error message string of course without any formatting.
It isn't much less readable than the <s:fielderror/> tag... after all we need to use property tags all the time anyways.
I had same issue I used following code to resolve my issue
<s:if test="fieldErrors.get('email').size() > 0">
<s:property value="fieldErrors.get('email').get(0)"/>
</s:if>
Where email is name of my field. This way we don't have to modify CSS.
Here is a tutorial to show the use of the Struts 2′s ActionError and ActionMessage class.
http://www.mkyong.com/struts2/struts-2-actionerror-actionmessage-example/
ActionError – is used to send error feedback message to user – display via < s:actionerror/ >
<s:if test="hasActionErrors()">
<div class="errors">
<s:actionerror/>
</div>
</s:if>
ActionMessage – is used to send information feedback message to user,display via < s:actionmessage/ >
<s:if test="hasActionMessages()">
<div class="welcome">
<s:actionmessage/>
</div>
</s:if>
I have a list of items, let's say types. I can display a <select> tag like so:
<form:select path="typeId" items="${types}" itemLabel="name" itemValue="typeId"/>
I would like to add the description property from the Type object to a data- attribute on the <option> tag for use with javascript.
This doesn't work, but just to provide an illustration:
<form:select path="typeId"/>
<form:options items="${types}" itemLabel="name" itemValue="typeId" data-description="description"/>
</form:select>
How can I gain access to the description property to populate the data-description attribute?
You need to implement your own tag (may on base of form:options), but as far as I know there is no concept of extending tags. (sorry)
data-description is not a valid attribute according to the TLD, so any custom attribute will potentially throw exception.
You can:
Build select and options using standard tag lib using c:forEach OR
Repurpose existing unused attribute for JavaScript. May be "title" attribute?
For example, a user will be rendered throughout my application as
<div class="user">
<p class="username">${user.name}</p>
<p class="karma">${user.karma}</p>
<img src="/users/${user.id}"/>
</div>
How can I reuse this code block?
Note - my code is running within a tag, so I can't use tags for this (or any JSP) otherwise I get a Scripting elements are disallowed here error.
Edit
I'm trying to use a tag file, but getting PropertyNotFoundException.
This is my tag file called 'user.tag':
<%#tag description="User" pageEncoding="UTF-8" %>
<a href="../user/showUser.do?userId=${user.id}">
<p>${user.name}</p>
<img class='avatar' src='${user.avatarUrl}' alt=""/>
</a>
And usage inside a jsp:
Where job.poster is a java bean with id, name, and avatarUrl properties.
If I add
<%#attribute name="user" %>
to the tag file, then I get an exception
Property 'id' not found on type java.lang.String
Since JSP 2.0, there is yet another kind of tags: Tag files. Tag files are JSP custom tags written as a JSP template itself, which seems to be what you want.
http://fforw.de/post/creating-jsp-layouts-with-page-tags/ shows how to use such a tag file as general layout solution. Using them as component should be even easier.
You should be able to use tag files within tag files; this works for me in a JSP 2.2 container:
<%-- mytag.tag --%>
<%#tag description="demo code" pageEncoding="UTF-8"%>
<%#taglib prefix="cust" tagdir="/WEB-INF/tags" %>
<%#attribute name="message"%>
<cust:mytag2 message="${message}" /><%-- uses mytag2.tag --%>
If that fails, you can use the include directive: <%#include file="/WEB-INF/jspf/fragment.jspf" %>
Note that the spec says about tags:
Directive Available? Interpretation/Restrictions
======================================================================
page no A tag file is not a page. The tag directive must
be used instead. If this directive is used in a
tag file, a translation error must result.
So, fragment.jspf must not have a any elements that are not supported in tags, including a page directive.
For the example you have given it sounds like some templating framework is needed, to display the user badge on each screen. At its simplest level this may just be a jsp:include which always includes your "UserBadge.jsp".
If you are running on a web framework e.g. JSF you may use Facelet templates or write a custom component for this. So the answer depends on what framework you have. Breaking it down to just JSP and JSTL - the included JSP or a javax.servlet.jsp.tagext.Tag would certainly reduce the duplication.
Always be careful to follow the DRY Principle... Don't Repeat Yourself!
I feel sometimes creating a .tag file is overkill (especially if it's only for one page), and I've wanted what you describe for years, so I wrote my own, simple solution. See here:
https://stackoverflow.com/a/25575120/1607642
Why don't you use a custom tag or jsp functions.
Thanks,