Struts html tags are not working - struts-1

I just tried to include a textbox in jsp page with struts provided tags. But it's behaving every typical.
Working
<html:text property="name" value=""></html:text>
Not Working
<html:text property="name"></html:text>
So, with out property 'value', its not working.
Please help me guys.
Note:
Using tomcat 6.0
bean, logics are working good and in html also most of them are working. radio, textarea, text are some which are causing issues.
JSP FILE
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%# taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%# taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%# taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<html:text property="name"></html:text>
</body>
</html>
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 14
11: <title>Registration Form</title>
12: </head>
13: <body>
14: <html:text property="name"></html:text>
15: </body>
16: </html>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:398)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot find bean: "org.apache.struts.taglib.html.BEAN" in any scope
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:852)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
org.apache.jsp.index_jsp._jspService(index_jsp.java:79)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
javax.servlet.jsp.JspException: Cannot find bean: "org.apache.struts.taglib.html.BEAN" in any scope
org.apache.struts.taglib.TagUtils.lookup(TagUtils.java:864)
org.apache.struts.taglib.html.BaseFieldTag.prepareValue(BaseFieldTag.java:126)
org.apache.struts.taglib.html.BaseFieldTag.renderInputElement(BaseFieldTag.java:102)
org.apache.struts.taglib.html.BaseFieldTag.doStartTag(BaseFieldTag.java:80)
org.apache.jsp.index_jsp._jspx_meth_html_005ftext_005f0(index_jsp.java:96)
org.apache.jsp.index_jsp._jspService(index_jsp.java:69)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.18 logs.
--------------------------------------------------------------------------------
Apache Tomcat/6.0.18

The <html:text> tag is supposed to be enclosed inside an <html:form> tag, which defines the action, and thus the ActionForm instance from which <html:text> gets its value.

Well This <Html:text> is the one kind of User input text which will get value form the userside and that will be assign further but value wont work in this tag.
that is suppose to be in the <html:form> because of that will have instance of action and that way action will be called.
you can assign that a hidden variable value.
so the structure will be
<html:form>
<html:text/>
</html:form>

In case anyone else stumbles on this post looking for a solution to the obtuse javax.servlet.jsp.JspException: Cannot find bean: "org.apache.struts.taglib.html.BEAN" in any scope error message, I caused it by accidentally inserting one Struts <html:form> inside of another <html:form>. The error message Struts doesn't indicate this. I figured it out by reviewing the HTML produced by my page.

Related

How to convert java code to jstl

I cannot found the way to convert java code to jstl
This is my java code
for(int i=0;i<listCategory.size();i+=3){
int size = i+3<listCategory.size() ? i+3 : listCategory.size
for(int j=i;j<size;j++){
Category cate = lisCategory.get(j);
}
}
Please help me :(
Hi I’m not sure if I understand what you want, but ill try to help you if I can, The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates core functionality common to many JSP applications. Below is the tag for the core libary.
<%# taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
If you want to emulate your for loop from Java you would use the <c:forEach> Tag. This will allow you to iterate over a list in your JSP page assuming you include the taglib provided above. Here is an example for you to view.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:forEach> Tag Example</title>
</head>
<body>
<c:forEach var="i" begin="1" end="5">
Item <c:out value="${i}"/><p>
</c:forEach>
</body>
</html>
I hope this gives you some insight or helps you along your journey.

Display a list by display:table tag in Spring + Hibernate MVC project

I am doing on a MVC project using netbean IDE and I have a problem with displaying my objects on table
This is my jsp page
<%#page import="java.util.List"%>
<%#page import="java.util.ArrayList"%>
<%#page import="model.Clothes"%>
<%#page import="org.hibernate.Session"%>
<%#page import="cfg.HibernateUtil"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%#taglib prefix="display" uri="http://displaytag.sf.net" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Clothes</title>
</head>
<body>
<div style="margin-left: 10%; ">
<%
Session session1 = HibernateUtil.getSessionFactory().getCurrentSession();
List<Clothes> clothes = new ArrayList<Clothes>();
session1.beginTransaction();
clothes = session1.createQuery("from Clothes").list();
session1.getTransaction();
System.out.println(clothes.size() + "aaaaaaaaa");
session1.close();
request.setAttribute("results", clothes);
%>
<display:table name="results" pagesize="10"/>
</div>
<jsp:include page="/index.htm" flush="true"/>
</body>
And the browser display an exception at line contains 'display' tag like this:"org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/jsp/clothes.jsp" Where did I do wrong, please help me, I am newbie in java web.
p/s: I add libs in my project, they are: displaytag-1.2, displaytag-export-poi-1.2, displaytag-portlet-1.2 and commons-lang-2.6.
I followed #JB Nizet instruction and it worked, just use the libs exactly the same that listed in this link: http://www.displaytag.org/1.2/displaytag/dependencies.html
However, in some PC, when using org.slf4j, your project can not work properly, just remove them from the project, it is OK!

JSP taglib to select file path - Undefined attribute name type

I am trying to create a form. Where the user need to select a file path.
This can be done in html as
<input type="file" name="inputFileName"/>
If we use html then I don't know how to map that value to "fileObject" class.
So how should I do this with the following code throwing a warning "Undefined attribute name type "
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="f" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<f:form method="PUT" commandName="fileObject" >
<f:input type="file" path="inputFileName"/>
</f:form>
</body>
</html>
Exception:
org.apache.jasper.JasperException: /WEB-INF/jsp/Main.jsp(9,0) Attribute type invalid for tag input according to TLD
f:input does not have attribute type as you can find it in docs

What's the best way to deliver both static and AJAX page content?

What is the best-practice for developing web-pages for both non-JS-enabled and JS-enabled pages? I am developing a Spring MVC web-application using JSP/JSTL as the view technology.
The way I work is to create a full web-page (with "html", "head", etc) tags in it. This will work on all browsers. My entire app works (however ugly-bugly) with JS disabled.
Also included in each page is some jQuery script that prettifies the page, typically turning top-level "divs" into tabs, other divs into dialogs, etc. The JS "hijacks" the underlying HTML. My JS hijacks the links and buttons, using AJAX calls to load the content into the correct dialog or tab div.
This all works nicely and I like the architecture, but I have added an optimisation such that the AJAX requests appends a "contentOnly" parameter; this is picked up by the Spring MVC view which conditionally ignores the "head", etc - i.e. it only renders the real content that the AJAX version wants. It just feels clunky.
I know I could load the entire page and discard the outer bits but this seems inefficient to load all the associated CSS and JS files which are not strictly necessary.
My question is: is there a nicer way to write this conditional formatting, should I be using a different view technology or something else, something like Velocity or FreeMarker or whatever Grails uses?
An example of my conditional: -
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:if test="${param.contentOnly == null}">
<!DOCTYPE html>
<html>
<head>
<%# include file="_stylesAndScripts.jsp"%>
<title>My App Title</title>
</head>
<body>
<%# include file="_header.jsp"%>
<%# include file="_menu.jsp"%>
</c:if>
<div id="leadListPanel" class="contentPanel">
<h1>My App Title</h1>
<p>The time on the server is ${serverTime}.</p>
<table id="leadTable" class="list">
... rest of content...
<c:if test="${param.contentOnly == null}">
<%# include file="_footer.jsp"%>
</body>
</html>
</c:if>
You need to include the files the other way round.
/WEB-INF/template.jsp
<!DOCTYPE html>
<html>
<head>
<%# include file="_stylesAndScripts.jsp"%>
<title>My App Title</title>
</head>
<body>
<%# include file="_header.jsp"%>
<%# include file="_menu.jsp"%>
<jsp:include page="${page}.jsp" />
<%# include file="_footer.jsp"%>
</body>
</html>
leads.jsp
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<div id="leadListPanel" class="contentPanel">
<h1>My App Title</h1>
<p>The time on the server is ${serverTime}.</p>
<table id="leadTable" class="list">
... rest of content...
And create a controller which sets leads as ${page} variable based on a specific request URL and forward to template.jsp instead. Since I don't do Spring, I can't answer in detail how to achieve it with Spring. But the following basic JSP/Servlet kickoff example should give the general idea:
#WebServlet(urlPatterns={"/pages/*"})
public class TemplateServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String page = request.getPathInfo();
// .. Do some other preprocessing if necessary .. and then:
request.setAttribute("page", page);
request.getRequestDispatcher("/WEB-INF/template.jsp").forward(request, response);
}
}
Invoke it as http://example.com/contextname/pages/leads to get the template displayed with leads.jsp included. Finally you should be able to invoke http://example.com/contextname/leads.jsp independently to get the sole template content.
I have used that kind of approach a few years ago with Freemarker and I don't know better way to do it.
What you could do is to create JSP-tags for those conditions so that they look nicer and hides the actual implementation. Something like this:
<t:NonAjax>
<!DOCTYPE html>
<html>
<head>
<%# include file="_stylesAndScripts.jsp"%>
<title>My App Title</title>
</head>
<body>
<%# include file="_header.jsp"%>
<%# include file="_menu.jsp"%>
</t:NonAjax>
<div id="leadListPanel" class="contentPanel">
<h1>My App Title</h1>
<p>The time on the server is ${serverTime}.</p>
<table id="leadTable" class="list">
... rest of content...
<t:NonAjax>
<%# include file="_footer.jsp"%>
</body>
</html>
</t:NonAjax>

Making appliication internationnalize using jstl tag

I am trying to Internationalize my application using JSTL tag in JSP. My problem is, the content of properties file are not visible in UI.
JSP CODE
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
This Example demonstrates the basic JSTL formating tags:
<br/><br/>
Locale from client:
<b><c:out value="${pageContext.request.locale}"/></b><br/>
<fmt:setBundle basename="ApplicationResources" var="mybundle"/>
<fmt:message key="welcome.message" bundle="${mybundle}">
<fmt:param value="${param.uname}"></fmt:param>
</fmt:message>
<b>Now testing <fmt:setLocale>tag:</b><br>
Creating a ResourceBundle with client locale and setting it to <i>mybundle1</i>
variable.<br>
<fmt:setBundle basename="ApplicationResources" var="mybundle1"/>
Setting the locale to<i>it</i>(italian).<br/>
<fmt:setLocale value="it"/>
Creating a ResourceBundle with <i>it</i>(italian) locale and setting it to <i>mybundle2</i> variable.<br><br>
<fmt:setBundle basename="ApplicationResources" var="mybundle2"/>
<b>Message using <i>mybundle1</i>:</b><br>
<pre>
<fmt:message bundle="${mybundle1}" key="welcome.message">
</fmt:message>
</pre>
<br>
<b>Message using <i>mybundle2</i>:</b><br>
<pre>
<fmt:message bundle="${mybundle2}" key="welcome.message">
</fmt:message>
</pre>
</body>
</html>
The rendered output is:
This Example demonstrates the basic JSTL formating tags:
Locale from client: en_US
???welcome.message??? Now testing <fmt:setLocale>tag:
Creating a ResourceBundle with client locale and setting it to mybundle1 variable.
Setting the locale toit(italian).
Creating a ResourceBundle with it(italian) locale and setting it to mybundle2 variable.
Message using mybundle1:
???welcome.message???
Message using mybundle2:
???welcome.message???
I believe this is what we need to start with. Create two files named:
ApplicationResources.properties
ApplicationResources_it.properties
Both should contain (at least) similar entry:
welcome.message=Hello,{0}
Remove these unnecessary declarations:
<fmt:setBundle basename="ApplicationResources" var="mybundle1"/>
<fmt:setBundle basename="ApplicationResources" var="mybundle2"/>
Move <fmt:setLocale value="it"/> to the top of your page. It will set the locale for the entire page either way, I don't think there is a way to mix languages (thank God).

Resources