Home > Java > Struts 2 Hello World

Struts 2 Hello World

Goal :

Login Form Struts2

Login Form Struts2

 

Hello Struts2

Hello Struts2

Steps:

  • Create Maven Project
  • Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>strutshelloworld</groupId>
<artifactId>net.ren.struts2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>net.ren.struts2</name>

<properties>
<struts2.version>2.5.2</struts2.version>
<log4j2.version>2.5</log4j2.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts2.version}</version>
</dependency>

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-config-browser-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-junit-plugin</artifactId>
<version>${struts2.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>

</plugins>
</build>
</project>

  • 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 Blank</display-name> 
<filter> 
<filter-name>struts2</filter-name> 
<filter-class> org.apache.struts2.dispatcher.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> 
  • Action class

package strutshelloworld.net.ren.struts2.actions;

import com.opensymphony.xwork2.ActionSupport;
/**
* The Class Login.
*/
public class Login extends ActionSupport {

/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;

/** The username. */
private String username;

/** The password. */
private String password;

/* (non-Javadoc)
* @see com.opensymphony.xwork2.ActionSupport#execute()
*/
public String execute() throws Exception {
if( getUsername()== null || getPassword() == null){
return "input";
}
else if (this.username.equals("admin") && this.password.equals("admin123")) {
return "success";
} else {
addActionError(getText("error.login"));
return "error";
}
}

/**
* Gets the username.
*
* @return the username
*/
public String getUsername() {
return username;
}

/**
* Sets the username.
*
* @param username the new username
*/
public void setUsername(String username) {
this.username = username;
}

/**
* Gets the password.
*
* @return the password
*/
public String getPassword() {
return password;
}

/**
* Sets the password.
*
* @param password the new password
*/
public void setPassword(String password) {
this.password = password;
}

}

  • Create properties file ( app-resources.properties )

copy this content into properties file

label.username= Username
label.password= Password
label.login= Login
error.login= Invalid Username/Password.
  • Create JSP files ( Login.jsp and Welcome.jsp )

Login.jsp


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

<h2>Struts 2 - Login Application</h2>

<s:actionerror />
<s:form action="login.action" method="post">
<s:textfield name="username" key="label.username" size="20" />
<s:password name="password" key="label.password" size="20" />
<s:submit method="execute" key="label.login" align="center" />
</s:form>
</body>
</html>

Welcome.jsp

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

<h2>Howdy, <s:property value="username" />...!</h2>

</body>
</html>
  • Create struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
 <constant name="struts.enable.DynamicMethodInvocation" value="false" />
 <constant name="struts.devMode" value="true" />
<!-- <constant name="struts.action.extension" value="html" / -->
 <constant name="struts.custom.i18n.resources" value="app-resources" />

 <package name="default" namespace="/" extends="struts-default">
 <default-action-ref name="login" />
 <action name="login" class="strutshelloworld.net.ren.struts2.actions.Login">
 <result name="success">/WEB-INF/jsp/Welcome.jsp</result>
 <result name="error">/WEB-INF/jsp/Login.jsp</result>
 <result name="input">/WEB-INF/jsp/Login.jsp</result>
 </action>
 </package>
 
</struts>
  • index.html

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

<body>

</body>
</html>

  • Project Structure
Project Structure

Project Structure

Project in github

Categories: Java Tags: , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: