SSH整合步驟三、Struts2單獨配置
阿新 • • 發佈:2018-12-09
這裡先單獨配置Struts2框架,測試Struts2框架功能是否可用,測試可以後再整合。
書寫一個簡單的Action類用於測試:
public class TestAction extends ActionSupport{
public String test() throws Exception {
System.out.println("測試中……");
return SUCCESS;
}
}
書寫配置檔案:
<?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="test" namespace="/" extends="struts-default"> <action name="testAction" class="com.hh.test.TestAction" method="test"> <result name="success">/test.jsp</result> </action> </package> </struts>
準備頁面檔案:
<%@ 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> <h1>Test Successful ! !</h1> </body> </html>
重要:最後別忘了,在專案的web.xml中配置Struts2框架核心過濾器:
<!-- 配置Struts2核心過濾器 --> <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>
到此,可以測試Struts2框架的功能了: