使用Maven搭建Struts2框架(小白)
阿新 • • 發佈:2019-01-27
本文主要是自己在嘗試使用Maven搭建Struts2框架,可能個別步驟不太規範,偏小白向,如有錯誤或不足,還望各位大佬提點。
一、建立Maven專案
1、選擇Maven Project
2、選擇maven-archetype-webapp
3、填寫Group Id、Artifact Id
[備註] 建立專案後,可能會出現index.jsp報錯提示,主要是缺jar包導致。
解決辦法:右擊專案》Build Path》Configure Build Path...》Java Build Path》Libraries》Add Library...》Server Runtime》Apache Tomcat v7.0(選擇,Finish)》Apply and Close 即可解決。
二、配置檔案
1、struts2核心包
由於個別依賴繼承與struts2-core,故pom.xml依賴只需以下即可。
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5.16</version> </dependency> <dependency> <groupId>org.apache.struts.xwork</groupId> <artifactId>xwork-core</artifactId> <version>2.3.34</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency>
2、配置web.xml
<?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>strutstest</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-class><!-- 低版本對應的class可能有細微差別 --> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
3、配置struts.xml
在src/main/resources目錄下新建一個檔案struts.xml
<?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>
<constant name="struts.i18n.encoding" value="UTF-8" />
<package name="default" extends="struts-default" >
<action name="user" class="com.eser.controller.UserAction"><!-- 與業務邏輯層Action對應 -->
<result name="SUCCESS" type="redirect">/success.jsp</result><!-- 重定向至success.jsp -->
</action>
</package>
</struts>
三、業務邏輯層Controller
本文采用繼承ActionSupport類並重寫excute方法來編寫。在目錄src/main/java下新建資料夾com.eser.controller後,新建UserAction類,程式碼如下:
package com.eser.controller;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
// HttpServletRequest request = ServletActionContext.getRequest(); //若使用request,重定向後,前端頁面無法獲得後臺傳值;
ServletContext servletContext = ServletActionContext.getServletContext();
servletContext.setAttribute("uid", "1");
servletContext.setAttribute("uname", "eserleung");
return "SUCCESS";
}
}
四、前端頁面JSP
在目錄src/main/webapp下新建success.jsp頁面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
<h2>使用者ID:${uid }</h2></br>
<h2>使用者名稱:${uname }</h2>
</body>
</html>
五、部署到Tomcat上,用瀏覽器訪問http://localhost:8080/struts2/user,success.jsp頁面可以獲UserAction頁面傳值則搭建成功。