JAVA - 【Struts2】執行過程及配置測試

struts框架獲取:https://struts.apache.org/download.cgi#struts2522


struts框架執行過程:



<一> 配置測試

0> 必要的jar包

1> 配置struts控制器(過濾器)

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<filter>
		<filter-name>struts-prepare</filter-name>
		<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class>
	</filter>

	<filter>
		<filter-name>struts-execute</filter-name>
		<filter-class>org.apache.struts2.dispatcher.filter.StrutsExecuteFilter</filter-class>
	</filter>


	<filter-mapping>
		<filter-name>struts-prepare</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<filter-mapping>
		<filter-name>struts-execute</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.jsp</welcome-file>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>

</web-app>

2> 配置struts.xml配置文件

☞ 注意:classpath同級目錄下

<?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="p1" extends="struts-default">
		<action name="hello" class="xyz.kuoa.web.action.HelloAction.Hello" method="sayHello">
			<result>/page/success.jsp</result>
		</action>
	</package>
</struts>

3> 處理Action

package xyz.kuoa.web.action.HelloAction;

public class Hello {
	
	public String sayHello() {
		System.out.println("Hello Struts!");
		return "success";
	}
	
}

4> 測試

 

 

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