struts2 Eclipse 中集成strust2開發框架實例


下面通過建立一個小的實例具體來說明Eclipse 集成struts2,以下實例采用的為 struts2 版本為 struts2 2.2.3.1 為應用.

1. 下載struts2的開發包

第一步: 在瀏覽器中輸入 http://apache.org

第二步:在apche的頁面項目中選擇struct 點擊連接進入相關頁面

第三步: 點擊download選擇下載 struct2 ,本例子中選擇 :struts2 2.2.3.1,下載完后解壓,lib中的為strut2在開發中用到的包.

 

 2. 建立WEB項目

第一步:打開Eclipse點擊[文件]->Dynamic Web Project,輸入相關信息如下圖:

  

第二步:引用java開發包tomcat包及struts2必備包.

   (1) 應用 tomcat服務器包文件

   A. 右鍵Struts2->點擊properties 然后選擇左側的 java build path ->libraries

      

   (2) 添加jre包文件

        A. 右鍵Struts2->點擊properties 然后選擇左側的 java build path ->libraries

   

添加完后點擊ok即可.

第三步:strust2開發lib包直接拷貝到WEB-INF/lib下面即可

 

實例的建立:

1. 首先介紹實例包含的文件: struts.xml, HelloWorldAction java類,web.xml ,HelloWorld.jsp,index.jsp

2. 內容如下:

(1) struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
   <package name="helloworld" extends="struts-default">
     
      <action name="hello" 
            class="com.northeasttycoon.struts2.HelloWorldAction" 
            method="execute">
            <result name="success">/HelloWorld.jsp</result>
      </action>
   </package>
</struts>

 (2) web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- northeasttycoon -->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Struts 2</display-name>
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher
      </filter-class>
   </filter>

   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>

 (3) HelloWorldAction.java

/**
 * @author NorthEastTycoon
 *
 */
package com.northeasttycoon.struts2;
public class HelloWorldAction{
   private String name;
   private String passWord;

   public String getPassWord() {
	return passWord;
}
public void setPassWord(String passWord) {
	this.passWord = passWord;
}
public String execute() throws Exception {
      return "success";
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
}

 (4) HelloWorld.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
 歡迎,   
<s:property value="name"/>登陸</br>
密碼為,   
<s:property value="passWord"/></br>
</body>
</html>

 (5) index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello World</title>
</head>
<body>
  <h1>Hello World From Struts2</h1>
  <form action="hello">
     <label for="name">Please enter your name</label>
     <input type="text" name="name"/><br/>
     <label for="passWord">Please enter your passWord</label>
     <input type="text" name="passWord"/><br/>
     <input type="submit" value="Say Hello"/>
  </form>
</body>
</html>

 以上為例子全部內容經過調試.

效果如下圖:

 

備注:

    作者:東北大亨

    博客:http://www.cnblogs.com/northeastTycoon/p/5617958.html                                                                   

版權為個人所有,歡迎大家轉載;但轉載時必須注明文章來源,且在文章開頭明顯處給明鏈接。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM