Thursday, August 15, 2013

Creating sample Struts 2 Web application and deploying on Tomcat 7

This blog is extension of Configuring Eclipse's Dynamic Web project for Struts 2 with Maven and deploying on Tomcat 7.. In the previous blog you would have learned how to configure an eclipse dynamic web project with maven. Now I will use the same project to configure a sample struts 2 application.


We are going to create a sample application, that will provide a login screen to the user and prompt them to login. Incase of error it will show the error page and do simple validation using struts.


Our Struts application will consist of following components:-

src/main/java/com.struts2.demo.action Actions:
  1. LoginAction extends ActionSupport - this action class will have the logic to validate the user name/pwd and direct the call.
  2. MissingAction extends ActionSupport - empty action class for redirecting empty requests to a missing page.

src/main/resources/ struts files:

  1. struts.xml file
  2. sampleapp.xml - included struts file, to break up the configuration file into multiple files


src/main/resources/com.struts2.demo.action resource files:

  1. LoginAction-validation.xml - struts validation xml file. Does simple required string validations.
  2. package.properties - localization support properties file

src/main/webapp/sampleapp jsp files:

  1. error.jsp
  2. Login.jsp
  3. Missing.jsp
  4. src/main/webapp/index.html

src/main/webapp/WEB-INF: web.xml

1 Action classes -  

LoginAction 

/**
 * 
 */
package com.struts2.demo.action;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author 
 *
 */
public class LoginAction extends ActionSupport {

public String execute() throws Exception {

        if (isInvalid(getUsername())) return INPUT;

        if (isInvalid(getPassword())) return INPUT;

        return SUCCESS;
    }

    private boolean isInvalid(String value) {
        return (value == null || value.length() == 0);
    }

    private String username;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    private String password;

    public String getPassword() {
        return password;
    }

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


}

MissingAction


package com.struts2.demo.action;

import com.opensymphony.xwork2.ActionSupport;


public class MissingAction extends ActionSupport {
}


2 Resource Files

LoginAction-validation.xml

<?xml version="1.0"?>
<!DOCTYPE validators PUBLIC
          "-//Apache Struts//XWork Validator 1.0.2//EN"
          "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
  <field name="username">
    <field-validator type="requiredstring">
      <message key="requiredstring"/>
    </field-validator>
  </field>
  
  <field name="password">
    <field-validator type="requiredstring">
      <message key="requiredstring"/>
    </field-validator>
  </field>

</validators>


package.properties

requiredstring=  ${getText(fieldName)} is required.
password= Password
username= User Name
Missing.message=  You have successfully logged in.

Error.message= Login/Pwd is wrong.

struts.xml


<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
  <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
  <constant name="struts.devMode" value="true"/>
  
  <include file="sampleapp.xml"/>
  
</struts>

sampleapp.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
  <package name="sampleapp" namespace="/sampleapp" extends="struts-default">
    <action name="Login_*" method="{1}" class="com.struts2.demo.action.LoginAction">
      <result name="input">/sampleapp/Login.jsp</result>
      <result type="redirectAction">Missing</result>
      <result name="error">/sampleapp/error.jsp</result>
    </action>

    <action name="*" class="com.struts2.demo.action.MissingAction">
      <result>/sampleapp/{1}.jsp</result>
    </action>
      
  </package>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="struts_blank" 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>Struts</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>index.html</welcome-file>
  </welcome-file-list>
</web-app>


3. JSP files

error.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>Login error</title></head>

<body>
<p>
    <s:text name="Error.message"/>
</p>

<ul>
    <li><a href="<s:url action="Login_input"/>">Sign On</a></li>
    
</ul>

</body>

</html>

Login.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Sign On</title>
</head>

<body>
<s:form action="Login">
    <s:textfield key="username"/>
    <s:password key="password" />
    <s:submit/>
</s:form>
</body>
</html>

Missing.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Sign On</title>
</head>

<body>
<s:form action="Login">
    <s:textfield key="username"/>
    <s:password key="password" />
    <s:submit/>
</s:form>
</body>

</html>

index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <META HTTP-EQUIV="Refresh" CONTENT="0;URL=sampleapp/Login_input.action">
</head>

<body>
<p>Loading ...</p>
</body>
</html>


Your project structure in Eclipse should look like this:


Now when you deploy this application on Tomcat, you can access it via url -  http://localhost:8080/strutsapplication/

This will direct you to URL - http://localhost:8080/strutsapplication/sampleapp/Login_input.action and show the login page.

If you enter 1 as the password, it leads to error page.

If you do not enter anything in login or pwd fields, then validation is kicked and error messages are shown.

No comments:

Post a Comment