2021年在Eclipse建立古老的Struts 1專案。
範例環境:
- Windows 64bit
- jdk 1.8.0_171
- Eclipse EE Version: 2019-03 (4.11.0)
- Tomcat 9.0.37
安裝Eclipse並設定好Tomcat Server,然後在Eclipse建立Maven Web專案。
新增Maven專案時archetype選擇maven-archetype-webapp
。
範例專案的archetype參數設定如下。
- [Group Id]:
com.abc
- [Artifact Id]:
struts-demo
- [Version]:
0.0.1-SNAPSHOT
- [Package]:
com.abc.demo
struts1-demo
專案建立好後開啟pom.xml
加入struts1相關的依賴如下。
<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>
範例專案的pom.xml
設定如下。
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>com.abc</groupId>
<artifactId>struts1-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>struts1-demo Maven Webapp</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</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>
</dependencies>
<build>
<finalName>struts1-demo</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using
Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
開啟web.xml
,把<web-app>
2.3改為3.1(因為2.3的isELIgnored
預設為true,JSP的EL會被當一般字串處理。)。加入Struts1的ActionServlet
的servlet mapping,任何以.do
結尾的url request都交由Struts1的ActionServlet
處理,並設定/WEB-INF/struts-config.xml
為Struts1的設定檔。首頁設為index.jsp
。
web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Struts1 Demo</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>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
在/src/main/webapp/WEB-INF
目錄新增struts-config.xml
檔為struts 1的配置檔。設定/hello
request 的action-mapping到com.abc.demo.action.HelloAction
及forward到/WEB-INF/pages/hello.jsp
頁面。
struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
</global-forwards>
<action-mappings>
<action path="/hello" type="com.abc.demo.action.HelloAction">
<forward name="hello" path="/WEB-INF/pages/hello.jsp" />
</action>
</action-mappings>
</struts-config>
在Java Resources的src/main/java
新增com.abc.demo.action.HelloAction
,繼承Struts 1的Action
並覆寫execute()
方法。
HelloAction
package com.abc.demo.action;
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 HelloAction extends Action {
@Override
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("HelloAction.execute()...");
String name = request.getParameter("name");
System.out.println("name=" + name);
request.setAttribute("name", name);
return mapping.findForward("hello");
}
}
HelloAction.execute()
方法根據struts-config.xml
的action-mapping設定收到/hello
request後取得url參數name
,然後轉交給hello.jsp
。
在/src/main/webapp/WEB-INF/pages
目錄下新增hello.jsp
。
hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<h1>Hello ${name}</h1>
</body>
</html>
完成以上設定後將專案加入Tomcat Server並啟動,然後在瀏覽器輸入http://localhost:8080/struts1-demo/hello.do?name=SkyWalker
會顯示以下結果。
範例專案結構如下。
沒有留言:
張貼留言