第一个struts2.0应用

不动Java很多年的人伤不起啊。。

Struts2.0已经出来很长时间了,是由WebWork和Struts社区联手创建的,相较于1.x版本的struts可谓产生了天翻地覆的变化。struts2.0的优点在apache的struts2主页上已经展示出来了。 > - Build!

  • Easy startup - Jumpstart new projects with our bootstrap tutorial and template application or Maven archetype.
  • Improved Design - Code clean against HTTP-independant framework interfaces.
  • Enhanced Tags - Code less with stylesheet-driven form tags that provide their own markup.
  • Stateful Checkboxes - Avoid special handling with smart checkboxes that know when they are toggled.
  • Flexible Cancel Buttons - Go directly to a different action on cancel.
  • First-class AJAX support - Add interactivity and flexibility with AJAX tags that look and feel just like standard Struts tags.
  • Easy Spring integration - Inject dependencies into Actions using Spring without glue code or red tape. (Plexus support also available.)
  • Enhanced Results - Do more with speciality results for JasperReports, JFreeChart, Action chaining, and file downloading.
  • POJO forms - No more ActionForms! Use any JavaBean to capture form input or put properties directly on an Action class. Use both binary and String properties!
  • POJO Actions - Use any class as an Action class – even the interface is optional!
  • Deploy!
    • Easy plugins - Add framework extensions by dropping in a JAR. No manual configuration required! Bundled plugins add support for JavaServer Faces, JasperReports, JFreeChart, Tiles, and more …
    • Integrated profiling - Peek inside Struts2 to find where the cycles are going!
    • Precise Error Reporting - Flip directly to the location and line of an error.
  • Maintain!
    • Easy-to-test Actions - Test Struts2 Actions directly, without resorting to mock HTTP objects.
    • Intelligent Defaults - Skip obvious and redundant settings. Most framework configuration elements have a default value that we can set and forget. Say it once!
    • Easy-to-customize controller - Customize the request handling per action, if desired. Struts2 only does what you want it to do!
    • Integrating Debugging - Research problem reports with built-in debugging tools.
    • Easy-to-tweak tags - Customize tag markup by editing a FreeMarker template. No need to grok the taglib API! JSP, FreeMarker, and Velocity tags are fully supported.

相关的构建文档apache也给出了,这里是一些示例猛击我。 准备工作: 除了具备相应的环境之外还要到apache struts官方网站下载struts2的Jar包。

软件配置: > - Java Version:1.7.0_02

  • IDE:Eclipse Java EE IDE for Web Developers
  • StrutsVersion:2.2.3.1
  • Web Contain:Tomcat 7.0.20

1、我们需要一个项目去承载Struts,新建一个普通的Dynamic Web Project。 2、导入所需要的Jar包到%Project_DIR%/WebContent/lib目录下。 > - struts2-core-2.2.3.1

  • xwork-core-2.2.3.1
  • ognl-3.0.1
  • freemarker-2.3.16
  • commons-logging-1.1.1

3、可有可无的一些操作,在建立Struts配置文件之前,最好配置好struts2的xml模板,这样可以便于以后建立项目。 Window==>Preference==>XML==>XML Files==>Editor==>Templates

单击New

下拉框中选择 New XML

Name:Struts2 XML

Description:Create a XML file for struts2.

Pattern:

<?xml version="1.0" encoding="${encoding}" ?>
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts><!-- Configuration for the default package. -->
	<package name="default" extends="struts-default">

	</package>
</struts>

3、在项目的src目录下新建一个名为struts.xml的文件,建立的时候模板我们选择”struts2 xml”。新建一个自己用来放action的包”com.abel.web.action”。 4、调整一下web.xml 使其将struts2框架包含到我的项目中。这里struts2和struts1.x有一些区别struts1.x是使用servlet,而struts2是使用的filter。

<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd" id="WebApp_ID" version="3.0">
	<display-name>MyStruts2Practice</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.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>

5、建立一个自己的action,这个类可以自定义但必须存在execute()方法以及以下的五种常量。 public static final String xxx=”xxx”

  • SUCCESS =”success“ —-表示执行成功,返回”success”
  • NONE=”none” —-表示执行成功,不显示结果页面
  • ERROR=”error” —-表示执行失败,返回”error”
  • INPUT=”input” —-表示action需要所要用户的更多信息,需要用户输入,返回”input”
  • LOGIN=”login” —-表示因为用户没有登陆action不能执行,向用户显示登陆页面,返回”login”

这个类通常可以实现com.opensymphony.xwork2.Action这个接口,现在看来java的命名规范没.NET好…… .NET接口都是以I开头。

package com.abel.web.action;

import com.opensymphony.xwork2.Action;

public class HelloAbelAction implements Action {

	private String msg;

	public String getMsg() {
		return msg;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		this.msg = "Hello Abel!";
		return SUCCESS;
	}
}

6、配置struts.xml,告诉页面应该去哪里找action,且执行之后根据步骤5中返回的五个常量去哪个页面。

xml version="1.0" encoding="UTF-8" ?>


<struts><!-- Configuration for the default package. -->
	<package name="default" extends="struts-default">
         <action name="HelloAbel" class="com.abel.web.action.HelloAbelAction">
         	<result name="success">/HelloAbel.jsp</result>
         </action>
	</package>
</struts>

7、建立一个名为HelloAbel的jsp页面,并从action中读取msg的数据。

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="mine" uri="/struts-tags"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello</title>
</head>
<body>
	<mine:property value="msg" />
</body>
</html>

7、将项目加入到tomcat 7.0中去跑跑,这时出现几个BUG……..

java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils

java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils

java.lang.NoClassDefFoundError: org/apache/commons/fileupload/RequestContext

java.lang.ClassNotFoundException: javassist.ClassPool

看来struts2 2.2.3.1 相较于老版本的struts2 也有点区别….
打开jar文件发现这几个类分别存在于commons-io-2.0.1.jar|commons-lang-2.5.jar|commons-fileupload-1.2.2.jar|javassist-3.11.0.GA.jar包中,果断加入到项目中。

至此呢,第一个Struts2应用完成了,与老版本struts2不同的是,Version:2.2.3.1 需要9个基本包儿……

Categories:

Updated: