Thursday 7 July 2016

STRUTS 2 IN ECLIPSE

Step 1:
           Create Dynamic Web Project(File->New->Dynamic Web Project)

Step 2:

          Add Required Jar Files(Right click the project-->build path-->configured build path-->Add External jars)




Step 3: 

           Create jsp file 

             <%@ page language="java" contentType="text/html; charset=ISO-8859-1"

                pageEncoding="ISO-8859-1"%>
             <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"                                    "http://www.w3.org/TR/html4/loose.dtd">
              <%@ taglib uri="/struts-tags" prefix="s" %>
                   <html>
                    <head>
                   <title>Insert title here</title>
                   </head>
                    <body>
                     <s:form action="login">
                     Name: <s:textfield name="name"></s:textfield>
                      Password<s:password name="password"></s:password>
                     <s:submit value="Login"></s:submit>
                    </s:form>
                    </body>
                     </html>

Step 4:  

 Create Action Class(LoginAction.java)

package com.jeni;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{ static final long serialVersionUID = 1L;
private String name,password;
//Getters and Setters(Right click tthis page Click Source->Generate Setters and Getters

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 String execute()
{
if(name.equals(password))
{
return SUCCESS;
}
else
{
return "ERROR";
}}}
Step 5:
            Create struts.xml file(web-inf-->classes-->struts.xml) and web.xml

struts.xml
<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation  
//DTD Struts Configuration 2.1//EN"   
"http://struts.apache.org/dtds/struts-2.1.dtd">  
<struts>  
<package name="abc" extends="struts-default">  
<action name="login" class="com.jeni.LoginAction">  
<result name="success">welcome.jsp</result>  
<result name="error">error.jsp</result>
</action>  
</package>  
</struts>   

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

  <display-name>StrutsValidation</display-name>
     <filter>  
  <filter-name>struts2</filter-name>  
   <filter-class>  
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
   </filter-class>  
  </filter>  
    <filter-mapping>  
   <filter-name>struts2</filter-name>  
    <url-pattern>/*</url-pattern>  
  </filter-mapping> 
    <welcome-file-list>
      <welcome-file>home.jsp</welcome-file>
  </welcome-file-list>
</web-app>

Step 6:
   
Create Success and Error Page

success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<body>
Welcome<s:property value="name"/></body>

error.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<body>
Incorrect Name and Password!!!!!!!!! </body>





2 comments: