Skip to content

Struts 1.x – Maven – Sample

March 26, 2012

For long time, was thinking to create a Struts Sample from the scratch using Maven. The first time when i used Struts, it was a Dynamic Web Project :). Afterwards, created Struts project from the scratch, but it is long time now almost 2-3 years and hence ventured out to create a new one.

The sample which i chose is http://www.allapplabs.com/struts/struts_example.htm

Maven Command-line to create a webapp

mvn archetype:generate -DgroupId=com.jagan.ss-DartifactId=strutssample -DarchetypeArtifactId=maven-archetype-webapp

pom.xml

<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd”&gt;
<modelVersion>4.0.0</modelVersion>
<groupId>com.jagan.strutsapp</groupId>
<artifactId>strutssample</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>strutssample Maven Webapp</name>
<url>http://maven.apache.org</url&gt;
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</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>

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts-extras</artifactId>
<version>1.3.10</version>
</dependency>

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

</dependencies>
<build>
<finalName>strutssample</finalName>
</build>
</project>

 

NameAction.java

 

package com.jagan.ss;

import java.io.IOException;
import javax.servlet.ServletException;
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;

public class NameAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String target = new String(“success”);
String name = null;
if (form != null) {
// Use the NameForm to get the request parameters
NameForm nameForm = (NameForm) form;
name = nameForm.getName();
}
// if no mane supplied Set the target to failure
if (name == null) {
target = new String(“failure”);
} else {
request.setAttribute(“NAME”, name);
}
return (mapping.findForward(target));
}
}

 

NameForm.java

package com.jagan.ss;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class NameForm extends ActionForm {
private String name = null;

public String getName() {
return (name);
}

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

public void reset(ActionMapping mapping, HttpServletRequest request) {
this.name = null;
}
}

 

struts-config.xml

<?xml version=”1.0″ encoding=”ISO-8859-1″ ?>
<!DOCTYPE struts-config PUBLIC
“-//Apache Software Foundation//DTD Struts Configuration 1.1//EN”
http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd”&gt;

<struts-config>
<form-beans>
<form-bean name=”nameForm”
type=”com.jagan.ss.NameForm” />

</form-beans>
<action-mappings>
<action path=”/Name” type=”com.jagan.ss.NameAction” name=”nameForm”
input=”/index.jsp”>
<forward name=”success” path=”/displayname.jsp” />
<forward name=”failure” path=”/index.jsp” />
</action>
</action-mappings>
</struts-config>

 

web.xml

<!DOCTYPE web-app PUBLIC
“-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”
http://java.sun.com/dtd/web-app_2_3.dtd&#8221; >

<web-app>
<display-name>Archetype Created Web Application</display-name>

<servlet>
<servlet-name>action</servlet-name>
<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>

</web-app>

 

index.jsp

 

<%@ page language=”java” %>
<%@taglib uri=”http://struts.apache.org/tags-html&#8221; prefix=”html”%>
<%@taglib uri=”http://struts.apache.org/tags-bean&#8221; prefix=”bean”%>

<html>
<head>
<title>Sample Struts Application</title>
</head>
<body>
<%– <html:form action=”Name” name=”nameForm” type=”com.jagan.ss.NameForm”> –%>
<html:form action=”Name”>
<table width=”80%” border=”0″>
<tr>
<td>Name:</td>
<td><html:text property=”name” /></td>
</tr>
<tr>
<td><html:submit /></td>
</tr>
</table>
</html:form>
</body>
</html>

displayname.jsp

 

<html>
<head>
<title>Sample Struts Display Name</title>
</head>
<body>
<table width=”80%” border=”0″>
<tr>
<td>Hello <%= request.getAttribute(“NAME”) %> !!</td>
</tr>
</table>
</body>
</html>

 

I hope you are aware of declaring the jetty plugin to run this application

 

Advertisements
No comments 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: