Alex刺客

          Dancing fingers, damage world. -- 舞動(dòng)手指,破壞世界.

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            57 隨筆 :: 0 文章 :: 76 評(píng)論 :: 0 Trackbacks
          唉我的第一篇日志真難發(fā)布,內(nèi)容剛剛寫完,不小心點(diǎn)了一個(gè)X火狐關(guān)了就沒(méi)了!
          一、在下載的struts2.1.6-all.zip找到以下包放入項(xiàng)目中WEB-INF/lib/下;

          1.commons-fileupload-1.2.1.jar   <--文件上傳
          2.commons-logging-1.0.4.jar   <--日志
          3.freemarker-2.3.13.jar  <--模板引擎
          4.ognl-2.6.11.jar <--Object-Graph Navigation Language
          5.struts2-core-2.1.6.jar   <--struts2核心
          6.xwork-2.1.2.jar  <--xwork核心

          二、在項(xiàng)目的web.xml文件中添加配置信息
           1 <?xml version="1.0" encoding="UTF-8"?>
           2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
           3   <display-name>Struts2HelloWord</display-name>
           4   
           5   <!--過(guò)濾器 -->
           6   <filter>
           7   
           8       <!-- 指定這個(gè)過(guò)濾的名字 -->
           9       <filter-name>struts2</filter-name>
          10       
          11       <!-- 指定這個(gè)過(guò)濾器的類路徑. 這個(gè)類是'struts2-core-2.1.6.jar'核心過(guò)濾分配器 -->
          12       <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
          13   </filter>
          14   
          15   <!--過(guò)濾器映射 -->
          16   <filter-mapping>
          17       
          18       <!-- 指定當(dāng)前所有請(qǐng)求至struts2過(guò)濾器 -->
          19       <filter-name>struts2</filter-name>
          20       <!-- 通配符*代表所有 -->
          21       <url-pattern>/*</url-pattern>
          22   </filter-mapping>
          23   
          24   <!-- 指定訪問(wèn)當(dāng)前項(xiàng)目的主頁(yè) -->
          25   <welcome-file-list>
          26     <welcome-file>index.jsp</welcome-file>
          27   </welcome-file-list>
          28 </web-app>

          三、新建一個(gè)名為HelloAction的Action類(此類在helloworld包中)
           1 package helloworld;
           2 
           3 public class HelloAction {
           4     
           5     //用于保存用戶名
           6     private String userName;
           7     //用于設(shè)置給welcome.jsp的信息
           8     private String massage;
           9     
          10     //setter getter方法
          11     public void setUserName(String userName){
          12         this.userName = userName;
          13     }
          14     
          15     public String getUserName(){
          16         return userName;
          17     }
          18     
          19     public void setMassage(String massage){
          20         this.massage = massage;
          21     }
          22     
          23     public String getMassage(){
          24         return massage;
          25     }
          26     
          27     //默認(rèn)處理用戶請(qǐng)求的方法
          28     public String execute() throws Exception{
          29         
          30         //判斷用戶名不能為空白
          31         if(!getUserName().equals("")){
          32             setMassage("scorpion剌客:"+getUserName()+"歡迎來(lái)到struts2的世界!");
          33             //在控制臺(tái)輸出massage成員
          34             System.out.println(getMassage());
          35             
          36             //返回success字符串
          37             return "success";
          38         }else{
          39             
          40             //返回error字符串
          41             return "error";
          42         }
          43     }
          44 }

          四、在src中新建一個(gè)struts.xml文件(如果你用文本編輯器struts.xml應(yīng)在項(xiàng)目/WEB-INF/classes下)
           1 <?xml version="1.0" encoding="UTF-8"?>
           2 
           3 <!--定義當(dāng)前xml文檔為struts2配置文檔  -->
           4 <!DOCTYPE struts PUBLIC
           5     "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
           6     "http://struts.apache.org/dtds/struts-2.1.dtd">
           7     <!-- 開(kāi)始配置struts2 -->
           8 <struts>
           9     <!-- 定義一個(gè)helloworld包,在此包的所有類都繼承struts-default包(此包在struts2-core-2.1.6.jar/struts-default.xml里)-->
          10     <package name="helloworld" extends="struts-default">
          11     <!-- 定義一個(gè)名為hello的Action 指定類文件為helloworld.HelloAction -->
          12         <action name="hello" class="helloworld.HelloAction">
          13         <!-- 當(dāng)helloworld.HelloAction類的execute方法返回success字符串給struts2框架時(shí),執(zhí)行welcome.jsp -->
          14         <!-- 以下默認(rèn)為<result name="success">……-->
          15             <result>/welcome.jsp</result>
          16         <!-- 當(dāng)helloworld.HelloAction類的execute方法返回error字符串給struts2框架時(shí),執(zhí)行error.jsp -->
          17             <result name="error">/error.jsp</result>
          18         </action>
          19     </package>
          20 </struts>

          五、新建一個(gè)index.jsp
           1 <%@ page language="java"  contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
           2 <%--struts2標(biāo)簽庫(kù)配置--%>
           3 <%@ taglib prefix="s" uri="/struts-tags" %>
           4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
           5 <html>
           6 <head>
           7 <title>歡迎</title>
           8 </head>
           9 <body>
          10     <%--向客戶端生成一個(gè)form表單 --%>
          11     <s:form action="hello">
          12         <%-- 生成一個(gè)text文本--%>
          13         <s:textfield name="userName" label="請(qǐng)問(wèn)你的名字是"/>
          14         <%-- 生成一個(gè)提交按鈕--%>
          15         <s:submit value="告訴他"/>
          16     </s:form>
          17 </body>
          18 </html>

          六、新建一個(gè)welcome.jsp
           1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
           2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
           3 <html xmlns="http://www.w3.org/1999/xhtml">
           4 <head>
           5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
           6 <title>welcome</title>
           7 </head>
           8 <body>
           9     
          10     ${requestScope.massage}
          11 </body>
          12 </html>

          七、新建一個(gè)error.jsp
           1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
           2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
           3 <html xmlns="http://www.w3.org/1999/xhtml">
           4 <head>
           5 <title></title>
           6 </head>
           7 <body>
           8     您不可能沒(méi)有名字!(偷偷告訴我可以啦!^_^)
           9 </body>
          10 </html>

          八、編譯 部署 訪問(wèn)
                
              這是我在http://www.aygfsteel.com/ 上第一篇日志希望能夠認(rèn)識(shí)更多的朋友!

                                                                                                          
          posted on 2009-07-01 21:16 Alex刺客 閱讀(499) 評(píng)論(0)  編輯  收藏 所屬分類: Struts2 Study Notes

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 泽普县| 灵丘县| 陆川县| 新兴县| 嘉禾县| 大名县| 梨树县| 祁阳县| 东阳市| 彰武县| 宜州市| 东辽县| 兴城市| 丹阳市| 崇阳县| 正宁县| 水富县| 晴隆县| 灌阳县| 辉县市| 文安县| 通化县| 包头市| 兴海县| 梅州市| 高阳县| 梁河县| 桐庐县| 元江| 婺源县| 廊坊市| 万安县| 蒙自县| 滕州市| 彭山县| 崇礼县| 平湖市| 渝北区| 会宁县| 大安市| 宝山区|