REGISTER FREE to Receive Instant Job

Sunday, 9 November 2014

struts 2 first web application with eclipse

Example of struts 2 first web application with eclipse....


Add Struts 2 Jar file(commons-logging-1.0.4,freemarker-2.3.8,ognl-2.6.11,struts2-core-2.0.11 and xwork-2.0.4)

Step 1: Create New Dynamic Web Project

Step 2: Filter Mapping Code for Communication between web.xml to struts.xml
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
<display-name>strutsDemo</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>
<!-- Filter Mapping Code for Communication between web.xml to struts.xml -->

<filter>
<filter-name>f1</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>f1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

Step 3: Create struts.xml 

struts.xml

<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<!-- Default struts package name-->

<package name="default" extends="struts-default">

<!-- Define action class-->
<action name="login" class="mypack.LoginAction"> <!-- Now create Action Class LoginAction.java -->
<!-- Define result page after success  to show welcome.jsp page or if any error than show index.jsp page-->

<result name="success">/welcome.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>


</struts>
Step 4: create two JSP pages welcome.jsp and index.jsp

welcome.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
 <!-- show error because library file not aviable, now add library jar file -->
<html>
<head>

</head>
<body>

<b> Welcome, <s:property value="name" /> </b>

</body>

</html>

 index.jsp

<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>

</head>
<body>
<!-- Create Login form  -->

<s:form action="login">
<!-- Define action to mapp with struts.xml -->
<s:textfield name="name" label="Name" />
<s:password name="password" label="Password" />
<s:submit value="login" />
</s:form>

</body>

</html>

Step 5: create Java action file

package mypack;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport { // Show error because required jar file
/** * */
private static final long serialVersionUID = 1L;
/*Define variable*/
private String name, password;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public void validate() {
if(name.length()==0)
this.addFieldError("name", "Name is required.");
if(password.length()==0)
this.addFieldError("password", "Password is required.");
else if(password.length()<4)
this.addFieldError("password", "Password should be of atleast 4 characters.");
}



}



Step 6: Right Click on Project and Run Program







4 comments:

Popular Posts

Powered by Blogger.

About Me

My photo

JobsAcid is a 'job search engine' to provide finding a job on the internet easier for the user. It has designed as huge selection of job offerings on the same search engine.