[struts2學習筆記] 第三節 建立struts 2 HelloWorld所需的六個步驟,struts2helloworld

來源:互聯網
上載者:User

[struts2學習筆記] 第三節 建立struts 2 HelloWorld所需的六個步驟,struts2helloworld

本文地址:http://blog.csdn.net/sushengmiyan/article/details/40349201

官方文檔: http://struts.apache.org/release/2.3.x/docs/hello-world-using-struts-2.html

本文作者:sushengmiyan

------------------------------------------------------------------------------------------------------------------------------------

當你在Struts 2 web應用中點擊一個超連結或者提交一個form表單資料時,輸入的資料並沒有傳到伺服器的另一個頁面,而是到了你提供的一個Java類. 這些類就叫做Actions. 當Action被fire, 結果會選擇一個資源呈現響應. 通常情況下,資源是一個網頁頁面, 也可以是PDF檔案, 或者是Excel頁簽, 或者是一個Java applet視窗.

假設你想建立一個簡單的"Hello World" 例子展示歡迎資訊. 在簡單設定struts工作環境之後(參照 如何建立struts 2 web應用), 建立"Hello World"例子,你需要做如下四件事情:

  1. 建立一個儲存歡迎資訊的java類(模型model)
  2. 建立一個呈現資訊的頁面(視圖 view)
  3. 建立一個Action類來控制使用者、模型和視圖之間的關係(控制器controller)
  4. 建立一個mapping (struts.xml) 來組合Action類和視圖
這篇文章假定你已經完成了 怎樣建立一個Struts 2 Web應用 體驗並且擁有一個基礎的struts工作空間. 這篇helloworld體驗的源碼,可以從Struts 2 GitHub 庫https://github.com/apache/struts-examples下載. 例子工程使用maven管理依賴和構造.war檔案.


第一步- 建立模型類 MessageStore.java

在我們第一個例子中,我們建立一個包com.susheng.struts2Maven.bean在裡面建立一個類MessageStore,內容如下:

package com.susheng.struts2Maven.bean;public class MessageStore {    private String message;        public MessageStore() {                 setMessage("Hello Struts User");    }     public String getMessage() {         return message;    }     public void setMessage(String message) {         this.message = message;    }}

第二步 - 建立Action類 HelloWorldAction.java

建立包com.susheng.struts2Maven.action在此包下建立類HelloWorldAction,內容如下:

package com.susheng.struts2Maven.action;import com.opensymphony.xwork2.ActionSupport;import com.susheng.struts2Maven.bean.MessageStore;public class HelloWorldAction extends ActionSupport{   private static final long serialVersionUID = 1L;       private MessageStore messageStore;         public String execute() throws Exception {                 messageStore = new MessageStore() ;        return SUCCESS;    }     public MessageStore getMessageStore() {        return messageStore;    }     public void setMessageStore(MessageStore messageStore) {        this.messageStore = messageStore;    }}



第三步 - 建立View HelloWorld.jsp

內容如下:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><%@ 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>    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">    <title>Hello World!</title>  </head>   <body>    <h2><s:property value="messageStore.message" /></h2>  </body></html>


第四步 - 增加Struts配置到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="basicstruts2" extends="struts-default">         <action name="index">            <result>/index.jsp</result>        </action>                <action name="hello" class="com.susheng.struts2Maven.action.HelloWorldAction" method="execute">          <result name="success">/HelloWorld.jsp</result>        </action>    </package> </struts>

第五步 - 建立URL Action

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><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Basic Struts 2 Application - Welcome</title></head><body><h1>Welcome To Struts 2!</h1><p><a href="<s:url action='hello'/>">Hello World</a></p></body></html>

第六步 - 構造WAR檔案並運行程式我構造的war包是basic_struts.war所以我訪問連結:http://localhost:8080/basic_struts/index.action,進入之後點擊helloworld連結,正常跳轉。



第二篇實際例子就學習完畢。


struts2的工作原理:



struts2 HelloWorld問題

你的項目沒有啟動吧
 
struts2 HelloWorld

第一步,安裝tomcat6.jdk1.6.(這是我的環境,應該不是必須的)
第二步,index.html,作用是跳轉到helloworld.action,代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=example/HelloWorld.action">
</head>

<body>
<p>Loading ...</p>
</body>
</html>
第三步,example目錄下的HelloWorld.jsp,代碼如下:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title><s:text name="HelloWorld.message"/></title>
</head>

<body>
<h2><s:property value="message"/></h2>

<h3>Languages</h3>
<ul>
<li>
<s:url id="url" action="HelloWorld">
<s:param name="request_locale">en</s:param>
</s:url>
<s:a href="%{url}">English</s:a>
</li>
<li>
<s:url id="url" action="HelloWorld">
<s:param name="request_locale">es</s:param>
</s:url>
<s:a href="%{url}">Espanol</s:a>
</li>
</ul>

</body>
</html>
第四步,example包......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.