Struts學習筆記(一):搭建struts

目錄

1.在web項目的lib文件夾中引入Struts-jar包

2.在web.xml中,引入Struts的核心功能

3.開發Action類

4.配置Action類

5.啓動tomcat服務器,打開瀏覽器驗證:


我的MyEclipse版本:

1.在web項目的lib文件夾中引入Struts-jar包

在Apache官網下載Struts:

分別下載jar包和源代碼,便於在myeclipse中,閱讀。

在Struts-2.5.18.all/struts-2.5.18/lib下是所有的jar文件(非常之多,以下是部分截圖):

只需在web-INF/lib下,導入核心的13個包即可,其他有需要再加:

並添加他們到編譯路徑中。

 

2.在web.xml中,引入Struts的核心功能

配置過濾器(加入如下信息:)

<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.xml內容變爲:

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>First_Struts</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>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

3.開發Action類

 

在src下新建一個類,模擬Action開發

package a_first_struts;
import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport{
	public String execute() throws Exception{
		System.out.println("訪問到HelloAction,開始處理請求");
		System.out.println("可以調用service處理");
		return "yes";
	}
}

4.配置Action類

在src下新建xml文檔:struts.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
    
<struts>
	<package name="hello" extends="struts-default">
	
		<action name="first_struts" class="a_first_struts.HelloAction" method="execute">
			<result name="yes">/index.jsp</result>
		</action>
		
	</package>

</struts>

5.啓動tomcat服務器,打開瀏覽器驗證:

同時,可以看到,控制檯的輸出:

 

至此,struts開發環境搭建完成。

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章