BAEL-524 Building an MVC application using Struts 2 (#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
This commit is contained in:
Ravi-ronic 2017-01-23 20:40:49 +05:30 committed by maibin
parent 8f590967c9
commit 7a4d666521
8 changed files with 152 additions and 1 deletions

View File

@ -182,6 +182,7 @@
<module>xmlunit2</module>
<module>xstream</module>
<module>java-mongodb</module>
<module>struts2</module>
</modules>
</project>
</project>

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>MyStrutsApp</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>
</web-app>

View File

@ -0,0 +1,21 @@
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Baledung Struts</title>
</head>
<body>
<form method="get" action="/MyStrutsApp/tutorial/car.action">
<p>Welcome to Baeldung Struts 2 app</p>
<p>Which car do you like !!</p>
<p>Please choose ferrari or bmw</p>
<select name="carName">
<option value="Ferrari" label="ferrari" />
<option value="BMW" label="bmw" />
</select> <input type="submit" value="Enter!" />
</form>
</body>
</html>

View File

@ -0,0 +1,11 @@
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<p> Hello Baeldung User </p>
<p>You are a <s:property value="carMessage"/></p>
</body>
</html>

44
struts2/pom.xml Normal file
View File

@ -0,0 +1,44 @@
<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>com.baeldung</groupId>
<artifactId>MyStrutsApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>MyStrutsApp</name>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.5</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,31 @@
package com.baeldung.struts;
public class CarAction {
private String carName;
private String carMessage;
private CarMessageService carMessageService = new CarMessageService();
public String execute() {
System.out.println("inside execute(): carName is" + carName);
this.setCarMessage(this.carMessageService.getMessage(carName));
return "success";
}
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public String getCarMessage() {
return carMessage;
}
public void setCarMessage(String carMessage) {
this.carMessage = carMessage;
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.struts;
public class CarMessageService {
public String getMessage(String carName) {
System.out.println("inside getMessage()" + carName);
if (carName.equalsIgnoreCase("ferrari"))
return "Ferrari Fan!";
else if (carName.equalsIgnoreCase("bmw"))
return "BMW Fan!";
else
return "please choose ferrari Or bmw";
}
}

12
struts2/src/struts.xml Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="tutorial" extends="struts-default" namespace="/tutorial">
<action name="car" class="com.baeldung.struts.CarAction"
method="execute">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>