can Struts Support HTML5? - java

I have been using struts framework i have to implement HTML 5 on the application is is possibke have makje changes in struts tag library?
since i have embeded all html tags like this
<html:form action="action.jsp" type="sample" name="applyform" >
can any one help?

There is no html5 specific taglib for Struts 1.x. (you tagged with 'struts', so I'm assuming that is what you are using). But you could write your own by extending the standard struts taglib. It shouldn't prove too difficult. Another option is to just use regular JSP EL/JSTL.

Sruts 2 supports HTML5 elements in its tag library, see for example the reference for textfield:
Type: (…) Specifies the html5 type element to display. e.g. text, email, url

Related

How to keep date data in a Struts form using html tags

I´m developing a Java EE application and I'm using Struts.
When I use html tags like
<input size="10px" id="start_date" name="start_date" type="text">
into my form, the ValidateError method (located in the FormBean class) doesn't maintain these dates after the error advise, returning and empty form.
I need to use this html tags rather than <forms:calendar...> (Struts tags) because we are using jQuery framework to improve the appearance.
Options :
You can use value=${param['start_date']} in input tag
You can still use struts tag and query element in jQuery using name or any other attribute.

Alternative to struts TextTag in spring or jsp custom tag

In struts custom tags we can generate HTML elements like <input> using TextTag.
Do we have any alternate to this in spring or jsp custom tags?
spring mvc has <form:input> which has similar features as <s:input> in struts
I digged more and here are my findings.
In Spring we can extend InputTag for customize results but limitation is element that you are specifying for value of input tag should be in commandBean. Otherwise will throw Bind error.
Second option is to create a JSP Tag with all flexibility, you can take some of implementation form InputTag and it works gr8.
I used second option.

How can I reuse code in JSP?

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,

Clean way for conditionally rendering HTML in view?

Is there a cleaner way to do this in a JSP/Struts1 setup ?
... some HTML here ...
EDIT: In admin mode I would like to have access to additional parameters from a form element,
e.g. from the form element:
input type="text" value="Test user" name="Owner"
EDIT 2: Actually, my problem is very similar to the question that was asked in : Conditionally Render In JSP By User
But I don't really get the "pseudo-code" from the likely answer
Is SessionConfig exposed as a bean in your JSP (as part of request / session / Struts Form)?
If it's not, you can expose it. And if it's a static class containing global settings (which, by the looks of it, is a possibility), you can create a small wrapper and put it in the servlet context which you'd then be able to access from Struts tags as scope="application".
Once that's done you can check your condition via Struts tags:
<logic:equal name="sessionConfig" property="adminMode" value="true">
... your HTML here
</logic:equal>
Or, if you're using EL / JSTL, same can be done via <core:if>.
Without more information, it's hard to answer this, but I'd think instead of separate views: one for admin mode, one for normal mode. Extracting the parts of your pages into tiles will help you do this without a lot of pain; see: http://tiles.apache.org/

Disable Struts converting HTML tags into entities

Hi I'm using Struts 1.2 and I installed TinyMCE. The problem is that struts is converting all HTML tags into entities. How do I disable or configure this to allow only selected tags?
Use the filter parameter (taglib).
<bean:write name="someField" filter="false"/>
Thanks, I was looking for this but in Struts 2, and this helped, it's similar:
<s:property value="variable" escape="false" />
I don't think that is a correct response for struts 2. At least on the later versions
escape property does not exist.
There are escapes for html, csv , javascript and xml which needs to be explicitly mentioned.
example for html, it has to be escapeHtml
<s:property value="%{somefield}" escapeHtml="false"/>

Categories