Quantcast
Viewing all articles
Browse latest Browse all 10

Struts Tag

In this Struts example, you will learn how to create a HTML password field with Struts <html:password> tag. In order to use the Struts HTML Tags you need to include the following taglib directive in the jsp page.

<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> 

First create a new Dynamic Web Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
  
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts-core</artifactId>
      <version>1.3.10</version>
    </dependency>
  
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts-taglib</artifactId>
      <version>1.3.10</version>
    </dependency>
  
</dependencies>

1. Create JSP

Now we will create a HTML password field using Struts’s HTML tag

Write the following code in login.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>Struts <Html:password> Password</title>
</head>
<body>
<html:form action="/login">
    User Name : <html:text property="userName"/>
    Password <html:password property="password" />
    <html:submit>Submit</html:submit>
</html:form>
</body>
</html>


Now create another JSP success.jsp that displays the text written in password field. Write the following code in success.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
 
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Success Page</title>
    </head>
    <body>
        Login Success. Welcome <bean:write name="LoginForm" property="userName"></bean:write>
        <br>Password Value :  <bean:write name="LoginForm" property="password"></bean:write>
         
    </body>
</html>

2. Action Form

Create a Action Form class and name it LoginForm.java that contains the following code.

package com.kruders.form;
 
import org.apache.struts.action.ActionForm;
 
public class LoginForm extends ActionForm{
    private static final long serialVersionUID = 1L;
    private String userName;
    private String password;
     
     
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public static long getSerialversionuid() {
        return serialVersionUID;
    }
}

3. Action Class (Controller)

Let us create an Action class LoginAction.java that will handles and forward the request.

package com.kruders.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;
 
import com.kruders.form.LoginForm;
 
public class LoginAction extends Action{
    public ActionForward execute(ActionMapping mapping,ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
                throws Exception {
        return mapping.findForward("success");
 
    }
}

4. Struts Config

Write the following code in struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
 
<struts-config>
     
    <form-beans>
        <form-bean name="LoginForm" type="com.kruders.form.LoginForm"/>
    </form-beans>
     
    <global-forwards>
        <forward name="login" path="/login.do"/>
    </global-forwards>
     
    <action-mappings>
        <action input="/login.jsp" name="LoginForm" path="/login"  validate="true" scope="request" type="com.kruders.action.LoginAction">
            <forward name="success" path="/success.jsp" />
        </action>
    </action-mappings>
 
</struts-config>

5. Deployment Descriptor

Now configure the deployment descriptor. Here, we have asked the container to give our ActionServlet any request that matches the pattern *.do

Add the following configuration information in the web.xml file

<welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
</welcome-file-list>
 
<servlet>
    <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>

6. Run Project

Now when you run the project, following screen will be displayed as shown in Figure 17.1

Image may be NSFW.
Clik here to view.
Figure 17.1
Figure 17.1


Now when you enter the value in password field and click submit button, following screen will be displayed as shown in Figure 17.2

Image may be NSFW.
Clik here to view.
Figure 17.2
Figure 17.2

The folder structure of the example is shown below in Figure 17.3

Image may be NSFW.
Clik here to view.
Figure 17.3
Figure 17.3

You can download the source code of this example here.

Viewing all articles
Browse latest Browse all 10

Trending Articles