Skip to content

Commit 7a4d666

Browse files
ravi-devgobletmaibin
authored andcommitted
BAEL-524 Building an MVC application using Struts 2 (eugenp#1010)
* BAEL-524 Building an MVC application using Struts 2 * BAEL-524 Building an MVC application using Struts 2 * BAEL-524 Building an MVC application using Struts 2
1 parent 8f59096 commit 7a4d666

8 files changed

Lines changed: 152 additions & 1 deletion

File tree

‎pom.xml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
<module>xmlunit2</module>
183183
<module>xstream</module>
184184
<module>java-mongodb</module>
185+
<module>struts2</module>
185186
</modules>
186187

187-
</project>
188+
</project>

‎struts2/WebContent/WEB-INF/web.xml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://java.sun.com/xml/ns/javaee"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
5+
id="WebApp_ID" version="3.0">
6+
<display-name>MyStrutsApp</display-name>
7+
<filter>
8+
<filter-name>struts2</filter-name>
9+
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
10+
</filter>
11+
12+
<filter-mapping>
13+
<filter-name>struts2</filter-name>
14+
<url-pattern>/*</url-pattern>
15+
</filter-mapping>
16+
</web-app>

‎struts2/WebContent/input.jsp‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7+
<title>Baledung Struts</title>
8+
</head>
9+
<body>
10+
<form method="get" action="/MyStrutsApp/tutorial/car.action">
11+
<p>Welcome to Baeldung Struts 2 app</p>
12+
<p>Which car do you like !!</p>
13+
<p>Please choose ferrari or bmw</p>
14+
<select name="carName">
15+
<option value="Ferrari" label="ferrari" />
16+
<option value="BMW" label="bmw" />
17+
</select> <input type="submit" value="Enter!" />
18+
19+
</form>
20+
</body>
21+
</html>

‎struts2/WebContent/result.jsp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<%@ page contentType="text/html; charset=UTF-8" %>
2+
<%@ taglib prefix="s" uri="/struts-tags" %>
3+
<html>
4+
<head>
5+
<title>Hello World</title>
6+
</head>
7+
<body>
8+
<p> Hello Baeldung User </p>
9+
<p>You are a <s:property value="carMessage"/></p>
10+
</body>
11+
</html>

‎struts2/pom.xml‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<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">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.baeldung</groupId>
4+
<artifactId>MyStrutsApp</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
<packaging>war</packaging>
7+
<name>MyStrutsApp</name>
8+
<build>
9+
<sourceDirectory>src</sourceDirectory>
10+
<resources>
11+
<resource>
12+
<directory>src</directory>
13+
<excludes>
14+
<exclude>**/*.java</exclude>
15+
</excludes>
16+
</resource>
17+
</resources>
18+
<plugins>
19+
<plugin>
20+
<artifactId>maven-compiler-plugin</artifactId>
21+
<version>3.5.1</version>
22+
<configuration>
23+
<source>1.8</source>
24+
<target>1.8</target>
25+
</configuration>
26+
</plugin>
27+
<plugin>
28+
<artifactId>maven-war-plugin</artifactId>
29+
<version>3.0.0</version>
30+
<configuration>
31+
<warSourceDirectory>WebContent</warSourceDirectory>
32+
</configuration>
33+
</plugin>
34+
</plugins>
35+
</build>
36+
<dependencies>
37+
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
38+
<dependency>
39+
<groupId>org.apache.struts</groupId>
40+
<artifactId>struts2-core</artifactId>
41+
<version>2.5.5</version>
42+
</dependency>
43+
</dependencies>
44+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.struts;
2+
3+
public class CarAction {
4+
5+
private String carName;
6+
private String carMessage;
7+
private CarMessageService carMessageService = new CarMessageService();
8+
9+
public String execute() {
10+
System.out.println("inside execute(): carName is" + carName);
11+
this.setCarMessage(this.carMessageService.getMessage(carName));
12+
return "success";
13+
}
14+
15+
public String getCarName() {
16+
return carName;
17+
}
18+
19+
public void setCarName(String carName) {
20+
this.carName = carName;
21+
}
22+
23+
public String getCarMessage() {
24+
return carMessage;
25+
}
26+
27+
public void setCarMessage(String carMessage) {
28+
this.carMessage = carMessage;
29+
}
30+
31+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.struts;
2+
3+
public class CarMessageService {
4+
5+
public String getMessage(String carName) {
6+
System.out.println("inside getMessage()" + carName);
7+
if (carName.equalsIgnoreCase("ferrari"))
8+
return "Ferrari Fan!";
9+
else if (carName.equalsIgnoreCase("bmw"))
10+
return "BMW Fan!";
11+
else
12+
return "please choose ferrari Or bmw";
13+
}
14+
15+
}

‎struts2/src/struts.xml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE struts PUBLIC
3+
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4+
"http://struts.apache.org/dtds/struts-2.0.dtd">
5+
<struts>
6+
<package name="tutorial" extends="struts-default" namespace="/tutorial">
7+
<action name="car" class="com.baeldung.struts.CarAction"
8+
method="execute">
9+
<result name="success">/result.jsp</result>
10+
</action>
11+
</package>
12+
</struts>

0 commit comments

Comments
 (0)