This is a demo for basic example of struts framework implementation in NetBeans IDE.
It is helpful to the developers who are looking for basic struts implementation. I have a step by step procedure with screen shots.
Have a look.
I) Create a project. File -> New Project
II) Give the project name and add the server. If server is not configured already then add the server like this
Tools -> servers ->
"add server"
Add the server if server is configured to the new project.
III)
Create a folder called “form” in “web pages” in the project
Create jsps welcomeStruts .jsp , succ.jsp and fail.jsp.
Creating welcomeStruts.jsp
Code:
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<html:html locale="true">
<body>
<html:form action="/register">
User Name <html:text property="userName"/>
Password <html:password property="password"/>
<html:submit/>
</html:form>
</body>
</html:html>
Creating succ.jsp
Creating fail.jsp
Creating Action form bean class
Right click on the package and select new action form bean class
Select those fields and right click on it. You can find like this.
Click “select all” and refactor button to get getters and setters.
Now Action form bean is like this
Click Next
Click “Finish”
Type the action code like this. This
If u hit . (dot) after the object it will show auto suggested methods like
Final action class is code
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.ActionMapping;
import org.apache.struts.action.ActionForward;
public class registerAction extends org.apache.struts.action.Action
{
private final static String SUCCESS = "success";
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
registerForm rf=(registerForm)form;
String username=rf.getUserName();
String password=rf.getPassword();
if(username.equals("sowjiavi") || password.equals("sowjiavi"))
{
return mapping.findForward(SUCCESS);
}else
return mapping.findForward("fail");
}
}
Struts-Config.xml
Step 1: Open the struts-config .xml file
Step 2: Add following content
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="registerForm" type="com.myapp.struts.registerForm"/>
</form-beans>
<action-mappings>
<action input="/" name="registerForm" path="/register" scope="session" type="com.myapp.struts.registerAction">
<forward name="success" path="/form/succ.jsp"/>
<forward name="fail" path="/form/fail.jsp"/>
</action>
</action-mappings>
</struts-config>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="registerForm" type="com.myapp.struts.registerForm"/>
</form-beans>
<action-mappings>
<action input="/" name="registerForm" path="/register" scope="session" type="com.myapp.struts.registerAction">
<forward name="success" path="/form/succ.jsp"/>
<forward name="fail" path="/form/fail.jsp"/>
</action>
</action-mappings>
</struts-config>
Then your struts-config.xml file look like this
web.xml
Step 1: Open web.xml file
Step 2: Modify the file like this.
Web.xml code is:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 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 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/form/welcomeStruts.jsp</welcome-file>
</welcome-file-list>
</web-app>
<web-app version="2.4" 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 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/form/welcomeStruts.jsp</welcome-file>
</welcome-file-list>
</web-app>
My directory structure is
Then compile and run the application
It automatically opens the browser if server is configured.
No comments:
Post a Comment