aspose file tools*
The moose likes Struts and the fly likes Struts basics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts basics" Watch "Struts basics" New topic
Author

Struts basics

Prabhat Kumar
Greenhorn

Joined: Jan 25, 2007
Posts: 27
Hi All,
I am facing a problem in Struts.I am doing the very first program for Struts.

The web.xml file is as follows:

<?xml version="1.0" encoding="ISO-8859-1"?>


<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web_app_2_4.xsd"
version="2.4">
<servlet>

<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>

</servlet>
<load-on-startup>1</load-on-startup>
<servlet-mapping>

<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>



</web-app>

and the Struts-config.xml is as follows:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://struts.apache.org/dtds/struts-config_1_1.dtd">

<struts-config>

<form-beans>
<form-bean name="lookupForm" type="LookupForm"/>
</form-beans>

<action-mappings>
<action path="/Lookup" type="LookupAction" name="lookupForm">
<forward name="success" path="/quote.jsp">
<forward name="failure" path="/index.jsp">
</action-mappings>
</struts-config>


I have created two JSP Files .

First is index.jsp

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html>
<head>
<title>
My First Struts Application
</title>
</head>
<body bgcolor="cyan">
<img src="images/exclam.gif">
<html:form action="Lookup" name="lookupForm" type="LookupForm">
Symbol:<html:text property="symbol"/><br>
<html:submit/>
</html:form>
</body>
</html>

and second id quote.jsp


<html>
<head><title><head>Quote.jsp</title></head>
<body bgcolor="green">
Cuurent Price:<%=request.getAttribute("PRICE")%>
</body>
</html>


The ActionForm class is as follows:

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.*;

public class LookupForm extends ActionForm
{
private String symbol;

public String getSymbol()
{
return symbol;
}

public void setSymbol(String symbol)
{

this.symbol=symbol;
}

public void reset(ActionMapping mapping,HttpServletRequest request)
{
this.symbol=null;
}

}


and Action class is as follows:


import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import java.io.*;

public class LookupAction extends Action
{
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws IOException ,ServletException
{
Double price=null;
String target=new String("success");
if(form!=null)

{
LookupForm lookupForm=(LookupForm)form;
String symbol=lookupForm.getSymbol();
price=getQuote(symbol);
}
if(price==null)
{
target=new String("failure");
}
else{
request.setAttribute("Price",price);
}
return (mapping.findForward(target));
}
public Double getQuote(String symbol)
{
if(symbol.equalsIgnoreCase("SUNW"))
{
return new Double(25.00);
}
return null;
}
}

I have kept 3 jar file struts.jar,commons-beanutils.jar ,and commomn-digester.jar in WEB-INF->lib folder
and struts-html.tld in WEB-INF folder.

classfile are under WEB-INF->classes and jsp's are under Context root.

I am very new to Struts,unable to troubleshoot.

When i am running my application exception comes on browser.Exception are


type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Exception in JSP: /index.jsp:10

7: </head>
8: <body bgcolor="cyan">
9: <img src="images/exclam.gif">
10: <html:form action="Lookup" name="lookupForm" type="LookupForm">
11: Symbol:<html:text property="symbol"/><br>
12: <html:submit/>
13: </html:form>


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:506)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

javax.servlet.ServletException: Cannot find ActionMappings or ActionFormBeans collection
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:843)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:776)
org.apache.jsp.index_jsp._jspService(index_jsp.java:82)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

javax.servlet.jsp.JspException: Cannot find ActionMappings or ActionFormBeans collection
org.apache.struts.taglib.html.FormTag.lookup(FormTag.java:798)
org.apache.struts.taglib.html.FormTag.doStartTag(FormTag.java:506)
org.apache.jsp.index_jsp._jspx_meth_html_form_0(index_jsp.java:100)
org.apache.jsp.index_jsp._jspService(index_jsp.java:72)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.20 logs.
.
I think it unable to find struts-config.xml.

One more problem regarding this iswhen i simple double click to view my struts-config.xml on my browser,it does not open but error comes which is
:
The system cannot locate the resource specified. Error processing resource 'http://struts.apache.org/dtds/struts-config_1_1...


Do i need to put struts-config_1_1.dtd somewhere.it is already inside the WEB-INF->lib.

Kindly help me out,i m in deep trouble.

Thanks in advance.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If you do not have connectivity to the internet while you are testing this application, the reason for the failure may be that the XML parser used by Struts can't find the DTD file. I'd suggest you change the path to the DTD in your struts-config.xml file so that it points to a local copy of the DTD. Example: file:///c:/foo/bar/struts-config_1_1.dtd


Merrill
Consultant, Sima Solutions
Prabhat Kumar
Greenhorn

Joined: Jan 25, 2007
Posts: 27
Thanks all.
My problem is ssolved now.ACtually i was writing the <load-on-startup> after closing the <servlet> tag.

Thank you all
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hi
Thats great that you found out the cause and resolved it.
But still, the <html:form> what you have used

<html:form action="Lookup" name="lookupForm" type="LookupForm">

should not (no need to) contain the name and type attributes. It does suffice if it contains only action.

Good luck!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts basics