struts2下的helloworld(如何讓第一個struts2跑起來)——struts2第一講

注:文章中的所有圖片均在附件中明確代表  
首先要安裝jdk1.6以及tomcat6和myeclipse 對於這些配置的安裝 這裏再也不細細說明 由於網上好些地方都有的 給個連接吧 http://blog.sina.com.cn/s/blog_5116f6310100b889.html 

其次是下載struts2 
第一步:去struts21的官網 http://struts.apache.org/2.1.6/index.html 
點擊下面圖1中的的download now,下載圖二中的全出便可。 下載後解壓待用; 

第二步; 
打開myeclipse tomcat的集成較簡單,很少講;新建一個web project 取名爲struts2; 
第三步; 
在剛剛下載的struts2-1-6目錄下的lib中複製出以下六個文件 
  commons-logging-1.0.4.jar 
  freemarker-2.3.8.jar  
  ognl-2.6.11.jar  
  struts2-core-2.0.6.jar 
  xwork-2.0.1.jar 
以及(由於是struts2-1-6版本的。因此一下這個文件也必不可少) 
commons-fileupload-1.2.1
 

而後粘貼到WebRoot/WEB-INF/lib便可; 
第四步: 
WebRoot目錄下新建一個login.jsp 
代碼以下 
login.jsp 
css

Jsp代碼  收藏代碼html

  1. <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>  java

  2. <%  web

  3. String path = request.getContextPath();  apache

  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  瀏覽器

  5. %>  tomcat

  6.   

  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  app

  8. <html>  eclipse

  9.   <head>  jsp

  10.     <base href="<%=basePath%>">  

  11.       

  12.     <title>My JSP 'login.jsp' starting page</title>  

  13.       

  14.     <meta http-equiv="pragma" content="no-cache">  

  15.     <meta http-equiv="cache-control" content="no-cache">  

  16.     <meta http-equiv="expires" content="0">      

  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  

  18.     <meta http-equiv="description" content="This is my page">  

  19.     <!--  

  20.     <link rel="stylesheet" type="text/css" href="styles.css">  

  21.     -->  

  22.   

  23.   </head>  

  24.     

  25.   <body>  

  26.     <form action="login.action" method="post">  

  27.         username: <input name="username" type="text"><br>  

  28.         password: <input name="password" type="password"><br>  

  29.           

  30.         <input type="submit" value="submit">  

  31.     </form>  

  32.   </body>  

  33. </html>  



第五步:
 
修改WEB-INF下的web.xml文件 
代碼以下 
web.xml 

Xml代碼  收藏代碼

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <web-app version="2.4"   

  3.     xmlns="http://java.sun.com/xml/ns/j2ee"   

  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   

  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   

  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  

  7.   

  8.     <filter>  

  9.         <filter-name>struts2</filter-name>  

  10.         <!-- 控制器 -->  

  11.         <filter-class>  

  12.         org.apache.struts2.dispatcher.FilterDispatcher  

  13.         </filter-class>  

  14.     </filter>                                                                 

  15.                

  16.                

  17.              <filter-mapping>  

  18.                 <filter-name>struts2</filter-name>  

  19.                 <!-- 任何請求均有過濾器 -->  

  20.                 <url-pattern>/*</url-pattern>  

  21.              </filter-mapping>  

  22. </web-app>  



第六步: 
新建action 
在src目錄下新建包com.test.action 
在包中新建一個action代碼以下 
LoginAction.java 

Java代碼  收藏代碼

  1. package com.test.action;  

  2.   

  3. public class LoginAction   

  4. {  

  5.       

  6.   

  7.                         //getter和setter方法   就是根據這裏的方法名來匹配客戶端的信息  

  8.     public String getUsername() {  

  9.         return username;  

  10.     }  

  11.     public void setUsername(String username) {  

  12.         this.username = username;  

  13.     }  

  14.     public String getPassword() {  

  15.         return password;  

  16.     }  

  17.     public void setPassword(String password) {  

  18.         this.password = password;  

  19.     }  

  20.       

  21.                public String execute() throws Exception  

  22.                {  

  23.                    return "success";  

  24.                }  

  25.       

  26.     //對應表單上的  

  27.     private String username;  

  28.     private String password;  

  29. }  



第七步: 
struts配置文件 

在src目錄下新建一個struts.xml代碼以下: 

Xml代碼  收藏代碼

  1. <?xml version="1.0" encoding="utf-8" ?>  

  2. <!DOCTYPE struts PUBLIC  

  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   

  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  

  5.               

  6. <struts>  

  7.                      

  8.                    <package name="struts2" extends="struts-default">  

  9.                             <action name="login" class="com.test.action.LoginAction">  

  10.                                         <!-- result沒有名字是默認的success -->  

  11.                                     <result name="success">/result.jsp</result>  

  12.                             </action>  

  13.                     </package>  

  14. </struts>  


注意,struts.xml中有句話是 
!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
"http://struts.apache.org/dtds/struts-2.0.dtd"> 

紅色這句可能會報錯,解決的方法是 將「http://」字樣去掉 其餘我不知道還有什麼方法,有高手知道請指點一二; 

第八步;
 
新建result文件 
在WebRoot目錄下新建一個result.jsp文件 代碼以下: 

Jsp代碼  收藏代碼

  1. ]  

  2. <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>  

  3. <%  

  4. String path = request.getContextPath();  

  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  

  6. %>  

  7.   

  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  

  9. <html>  

  10.   <head>  

  11.     <base href="<%=basePath%>">  

  12.       

  13.     <title>My JSP 'result.jsp' starting page</title>  

  14.       

  15.     <meta http-equiv="pragma" content="no-cache">  

  16.     <meta http-equiv="cache-control" content="no-cache">  

  17.     <meta http-equiv="expires" content="0">      

  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  

  19.     <meta http-equiv="description" content="This is my page">  

  20.     <!--  

  21.     <link rel="stylesheet" type="text/css" href="styles.css">  

  22.     -->  

  23.   

  24.   </head>  

  25.     

  26.   <body>  

  27. helloworld  

  28.         username: ${requestScope.username }<br>  

  29.         password: ${requestScope.password }  

  30.   </body>  

  31. </html>  



至此,已經完成了代碼的書寫工做。接下去是發佈; 
右鍵點擊struts2這個項目的名稱,在菜單中選擇myeclipse,在選擇add and remove project……便可,以後將出現圖三 
選擇project,點擊add發佈到指定的tomcat便可、 

最後,打開瀏覽器。在瀏覽器 http://localhost:8080/struts2/login.jsp 便可 


附件中有圖片和兩個源代碼;

struts2的類型轉換——Struts2第二講