structs2下的第一個helloworld!
下載安裝好structs21.創建並配置好web應用程序,使支持structs2:
將structs2/lib下的jar文件除了Junit,sprint-test.jar都拷貝到web-inf/lib下麵
2.配置web應用的web.xml文件。攔截所有url,使用struct2
<?xml version="1.0" encoding="GBK"?> <web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://java.sun.com/xml/ns/javaee" xmlns:web="https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!-- 定義Struts2的核心Filter --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- 讓Struts2的核心Filter攔截所有請求 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
3.創建一個hello.jsp ,在裏麵輸入helloworld!
4.在src目錄下創建一個structs.xml,配置struts下跳轉到hello.jsp
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE struts SYSTEM "https://struts.apache.org/dtds/struts-2.1.7.dtd" PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"> <!-- 指定Struts 2配置文件的根元素 --> -<struts> <!-- 指定全局國際化資源文件 --> <constant value="mess" name="struts.custom.i18n.resources"/> <!-- 指定國際化編碼所使用的字符集 --> <constant value="GBK" name="struts.i18n.encoding"/> <!-- 所有的Action定義都應該放在package下 --> <package name="default" extends="struts-default"> <action name="hello" > <!-- 定義邏輯視圖和物理資源之間的映射 --> <result name="input">/hello.jsp</result> </action> </package> </struts>
好了,這樣在瀏覽器中輸入:localhost:8080/web項目名稱/hello
就可以看到helloworld!了。
最後更新:2017-04-02 17:09:28