在Hello Struts中,將展示最基本的Struts的配置
後續的學習都在這個基礎上進行
1、在eclipse中創建Web動態項目struts
在eclipse中新建項目struts,使用dynamic web project的方式。
2、導入jar包
相關jar在文章後面我項目的lib目錄下,需要的可以自行下載
3、新建web.xml
在WEB-INF目錄下新建web.xml
其中配置了一個 Filter, 所有的請求都被過濾給了這個 Filter
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4、配置struts.xml在src目錄下創建一個struts.xml文件
這是最簡單的struts.xml配置
其效果是當訪問index路徑的時候,服務端跳轉到index.jsp
<?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="basicstruts" extends="struts-default">
<action name="index">
<result>index.jsp</result>
</action>
</package>
</struts>
5、創建index.jsp
在web目錄下創建index.jsp,
其內容很簡單 就是一句:Hello Struts2 World
<%@ 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>Hello Struts2 World</title>
</head>
<body>
Hello Struts2 World
</body>
</html>
6、部署在Tomcat中,重啓tomcat
部署在Tomcat中,重啓tomcat,然後訪問地址,觀察效果
http://127.0.0.1:8080/struts/index
相當於實現了servlet的服務端跳轉到index.jsp的功能7、思路圖
(1) 所有的訪問都回被web.xml中配置的Struts 的 Filter所攔截
(2) 攔截之後,就進入struts的工作流程
(3)訪問的地址是/index,根據struts按照 struts.xml中的配置,服務端跳轉到index.jsp
(4)顯示index.jsp的內容
ok,至此,最簡單的struts項目已經部署起來了。
源碼:鏈接:https://pan.baidu.com/s/1bq6B6ov 密碼:g2g4