Struts2學習之旅一 你妹的,這么多框架,為啥要用struts2?


1,strust2可以做神馬?可帶來哪些便利?

  首先,它是一個基於MVC的web應用框架,基於struts1和webwork,合並優化之后的產物。

 

     對於廣大用戶給它的贊美,這里我簡單的翻譯一下apache網站下的兩段話。

 

Apache Struts 2 is an elegant, extensible framework for creating enterprise-ready Java web applications. The framework is designed to streamline the full development cycle, from building, to deploying, to maintaining applications over time.

Apache Struts 2 was originally known as WebWork 2. After working independently for several years, the WebWork and Struts communities joined forces to create Struts2. This new version of Struts is simpler to use and closer to how Struts was always meant to be.

阿帕奇的struts2項目是一個優雅的,可擴展的框架,可以用來創建為企業准備的javaWeb應用,這個框架被設計來簡化整個開發的周期,從構建到部署到維護,貫穿整個軟件生命周期的時間段。

阿帕奇struts2最初被叫做WebWork2,獨立工作數年之后,webWork和Struts的交流合作促進創造了strust2,這個新版本的struts簡化了使用,跟之前我們想要使用的struts很接近。

 

翻譯的不好,見諒。從這兩段話看出,其實它主要就兩個價值點,一個是可擴展性,第二個就是簡化了web應用的開發周期。

 

我想隨着使用的深入,這兩大致命的誘惑會漸漸展現出它的豐姿...

 

2,hello world!

 

 
步驟

第一步:建立一個maven的web項目;

使用myEclipse建立一個maven的web項目,增加源碼文件夾,測試源碼文件夾;

來點圖片解釋下:

 

 

第二步:引入必要的依賴;

依次引入日志的依賴,測試的依賴,struts2的核心依賴;

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.7</version>
</dependency>
</dependencies>

第三步:配置web.xml;

到struts2的app中copy一個web.xml的頭,然后配置核心過濾器,以及系統的首頁;

<?xml version= "1.0" encoding = "UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>研究strust2的使用</display-name>

<context-param>
<param-name>appName</param-name>
<param-value>com.lifc.web.struts2.study</param-value>
</context-param>


<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

</web-app>

第四步:編寫struts.xml;

到struts的app下copy一個簡單的過來,然后配置用戶的請求和處理類的對應關系。

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />

<package name="default" namespace="/" extends="struts-default">

<default-action-ref name="index" />

<global-results>
<result name="error">/error.jsp</result>
</global-results>

<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>

<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>

<include file="example.xml"/>

<!-- Add packages here -->

</struts>

 

配置訪問url和系統的處理類的映射關系;

第五步:MVC ,按照這個順序編寫代碼;

首先編寫模型,義務處理代碼;

然后編寫視圖,盡量利用struts2提供的ognl標簽庫和表達式;

最后編寫控制器,完成url和視圖的對應關系映射和邏輯處理;

 

這個helloWorld沒有模型,先來個頁面:

 

<%@ page language="java" pageEncoding="UTF-8"%>
<%--
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
--%>

<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title><s:text name="HelloWorld.message"></s:text> </title>

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

</head>

<body>

<h1> <s:property value="message"/></h1>
<br>
<s:url id="url1" action="HelloWorld">
<s:param name="request_locale">en</s:param>
</s:url>
<s:a href="%{url1}">英語</s:a>
<br>
<s:url id="url2" action="HelloWorld">
<s:param name="request_locale">es</s:param>
</s:url>
<s:a href="%{url2}">阿拉伯語</s:a>
</body>
</html>

 

然后來個簡單的控制器:

繼承ActionSupport,簡單的返回succes,直接跳轉;

 

package example;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {

private static final Logger log=Logger.getLogger(HelloWorld.class);

private static final String MESSAGE="helloWorld.message";

private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

@Override
public String execute() throws Exception {

log.info("HelloWorld.Action 請求進來了...");

setMessage(getText(MESSAGE));

return SUCCESS;
}

}

第六步:測試,運行;

在測試源碼文件夾中測試義務代碼的正確性;

然后,進行系統的功能測試和性能測試(確保大數據量和訪問量的速度,以及健壯性穩定性)

這個例子無業務,直接是一個跳轉,到一個jsp,這里不再多做描述。

 
 
 

 

 

3,運行過程

  

 

4,小結小結,簡短的記憶下。

    這個開篇的小文章簡單的解釋了三個問題,strust2有什么優勢?動手做一個基於struts2的java web例子;大致的了解下struts2的運興流程;這個是以后進行struts2開發的基礎和目標,先簡單入門,再慢慢搞點小花樣,做一個像樣的站出來玩玩···歡迎各位struts2高手斧正。

 

15:00:502013-06-08  

       


免責聲明!

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



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