FIRST JAVA STRUTS EXAMPLE
Java struts framework is the de facto standart for the development. Lots of framework technologies which are arised after struts framework originated from this. This framework has basic rules which are very useful at the time of developing. It prevents you from repetitive code segments. Makes your time more valuable. For the first project that we will create have the structure such as below :
! ) As an addition to your dynamic web project, it will contain ActionForm Action config, web.xml parameters and additional jars.
The structure is easy
!! ) Your user interface file such as jsp will get the information from user and it will route to the related action defined in config when submitted by the user,
!!!)For this small application, the basic jar files are
struts.jar --> http://www.java2s.com/Code/Jar/s/Downloadstrutsjar.htm
commons-digester.jar http://grepcode.com/snapshot/repo1.maven.org/maven2/commons-digester/commons-digester/2.1
commons-logging.jar http://grepcode.com/snapshot/repo1.maven.org/maven2/commons-logging/commons-logging/1.1
commons-beanutils.jar http://www.findjar.com/jar/commons-beanutils/commons-beanutils/1.7.0/commons-beanutils-1.7.0.jar.html;jsessionid=BC4AB59D53DBE71503583342658DCE49
-->Your jsp file will have a html form which contains text, radiobutton, dropdown boxes... etc... and your jsp file will show an action path with the path attribute of the form.
--> The path will be handled in config file which will show the route action with type parameter and will send an form with the name parameter.This form will be filled by the additional jars through the information that user entered.
--> Action will then handle the request and will get a default form and it should cast to the appropriate form before using it. Now that is ok lets code
your jsp file
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
</head>
<body>
<html:form action="/actionPath" method="post">
<html:text property="property1"></html:text>
</html:form>
</body>
</html>
pay attention to the action="/actionPath" and property="property1"
This is your struts-config.xml file (Which should be near the web.xml file)
(Here as you can see there is an actionPath definition, this will handle and will create a bean that is ActionFrm and will send the user data to this object and then the path ActionPth will be executed.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
id="WebApp_ID"
version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
<display-name>
Struts
</display-name>
<welcome-file-list>
<welcome-file>
index.html
</welcome-file>
<welcome-file>
index.htm
</welcome-file>
<welcome-file>
index.jsp
</welcome-file>
<welcome-file>
default.html
</welcome-file>
<welcome-file>
default.htm
</welcome-file>
<welcome-file>
default.jsp
</welcome-file>
</welcome-file-list>
-
<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>
<load-on-startup>
1
</load-on-startup>
</servlet>
-
<servlet-mapping>
<servlet-name>
action
</servlet-name>
<url-pattern>
*.do
</url-pattern>
</servlet-mapping>
</web-app>
There is new definition for the web.xml which is all the .do url requests will be handled by a servlet named as action and the action servlet is the org.apache.struts.action.ActionServlet which is in the struts. And we say to the application that we will store our config file at the WEB-INF folder and will be named as struts-config.xml file.
This is your ActionForm under right package
package action;
import org.apache.struts.action.ActionForm;
public class ActionFrm extends ActionForm{
private String property1;
public String getProperty1() {
return property1;
}
public void setProperty1(String property1) {
this.property1 = property1;
}
}
And this is your Action class that will handle all the coming request with the appropriate function which is execute function. That function must be overriden by you.
package action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class ActionPth extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
ActionFrm action_form = (ActionFrm) form;
String reqData = action_form.getProperty1();
System.out.println(reqData);
return null;
}
}
Now if you run your application from eclipse you will see below pictures.



No comments:
Post a Comment