Struts2—-HelloWorld

來源:互聯網
上載者:User

設定檔主要有

Struts.xml

web.xml

 

其中Stuts.xml

 1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4     "http://struts.apache.org/dtds/struts-2.3.dtd"> 5  6 <struts> 7  8     <constant name="struts.devMode" value="true" /><!--true表示現在為開發模式 --> 9     <!-- 即修改配置不需要重啟伺服器 -->10     <package name="default" namespace="/" extends="struts-default">11         <action name="hello">12             <!-- result 即Action處理後返回給使用者的師徒資源,下面配置了一個result -->13             <!-- result不填寫任何屬性時,預設觸發條件為SUCCESS -->14             <result>/Hello.jsp</result>15         </action>16     </package>17 18 19 </struts>

 

 

Web.xml

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5"  3     xmlns="http://java.sun.com/xml/ns/javaee"  4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7   <display-name></display-name>     8  9       <!-- 載入Struts 2的核心控制器 用來對事件作出處理 -->10       <filter>11         <filter-name>struts2</filter-name>12         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>13     </filter>14     <!-- 使用萬用字元/*來攔截所有的URL請求,保證使用者請求都被Struts2截獲 -->15     <filter-mapping>16         <filter-name>struts2</filter-name>17         <url-pattern>/*</url-pattern>18     </filter-mapping>19 20 </web-app>

 

 

Hello.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6  7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9   <head>10     <base href="<%=basePath%>">11     12     <title>Struts2</title>13     <meta http-equiv="pragma" content="no-cache">14     <meta http-equiv="cache-control" content="no-cache">15     <meta http-equiv="expires" content="0">    16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">17     <meta http-equiv="description" content="This is my page">18     <!--19     <link rel="stylesheet" type="text/css" href="styles.css">20     -->21   </head>22   23   <body>24     Hello Struts2. <br>25   </body>26 </html>

 

 

當向瀏覽器輸入地址時,根據網域名稱進入package搜尋對應action.而後執行result中相應的result

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.