Solution:
The files used in this exercise are:
1. test.jsp
2. searching.jsp
3. BookForm.java
4. BookAction.java
<%@ taglib uri = "/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head> <title>Struts</title>
</head>
<body>
<html:form action="book.do" ><center>
<b>TITLE:<b> <html:text property ="title" /><p>
<b>AUTHOR:<b> <html:text property ="author" /><br> <p>
<html:submit /></center>
</html:form>
</body>
</html>
Enter the code in Notepad and save the file as test.jsp in %TOMCAT_HOME%/webapps/ struts-test.
<html>
<head><title>Searching</title>
</head>
<body>
<h3><b>Searching.....<b></h3>
</body>
</html>
Enter the code in Notepad and save the file as searching.jsp in %TOMCAT_HOME%/webapps/ struts-test.
package com;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class BookForm extends ActionForm
{
private String nTitle = null;
private String nAuthor = null;
public String getTitle() { return nTitle; }
public void setTitle(String aTitle) { nTitle = aTitle; }
public String getAuthor() { return nAuthor; }
public void setAuthor(String aAuthor) { nAuthor = aAuthor; }
public void reset(ActionMapping aMapping, HttpServletRequest aRequest)
{
nTitle = null;
nAuthor = null;
}
}
Enter the Java code in Notepad and save the file as BookForm.java. Compile the file from the command prompt and copy the class file in %TOMCAT_HOME%/webapps/struts-test/WEB-INF/classes/com.
package com;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class BookAction extends Action
{
public ActionForward perform(ActionMapping aMapping, ActionForm aForm,HttpServletRequest aRequest, HttpServletResponse aResponse)throws ServletException
{
BookForm f = (BookForm) aForm;
String title = f.getTitle();
String author = f.getAuthor();
return aMapping.findForward("saved");
}
}
Enter the Java code in Notepad and save the file as BookForm.java. Compile the file from the command prompt and copy the class file in %TOMCAT_HOME%/webapps/struts-test/WEB-INF/classes/com.
Enter the Java code in Notepad and save the file as BookAction.java. Compile the file from the command prompt and copy the class file in %TOMCAT_HOME%/webapps/struts-test/WEB-INF/classes/com.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
<struts-config>
<!-- Form Bean Definitions -->
<form-beans>
<form-bean name = "bookForm" type = "com.bookForm"/>
</form-beans>
<!-- Global Forward Definitions -->
<global-forwards>
<forward name = "start" path = "/index.jsp"/>
</global-forwards>
<!-- Action Mapping Definitions -->
<action-mappings>
<action path = "/book" type = "com.bookAction" name = "bookForm">
<forward name = "saved" path = "/greeting.jsp"/>
</action>
</action-mappings>
</struts-config>
Update the struts-config.xml file used in the Web application.
The output of the program is as shown in Figure 16.1.
Figure 16.1: Input page
After the user clicks the Submit button, the output of the program is as shown in Figure 16.2.
After the user clicks the Submit button, the output of the program is as shown in Figure 16.2.
0 nhận xét:
Post a Comment