struts2学习之一

纯属记录。

由于工作中的项目需要用到struts2,就花了点时间作了些了解和练习,为了加深印象记录之。
大步骤如下:
1、使用gradle构建项目;
2、一个简单的action;
3、使用json格式数据传输数据 http://arthur2014.iteye.com/admin/blogs/2162989
4、spring注解 http://arthur2014.iteye.com/admin/blogs/2162996
5、struts2注解 http://arthur2014.iteye.com/admin/blogs/2163348


一、使用gradle构建项目
1、创建一个Dynamic Web Project;
2、修改.classpath文件,如:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
	<classpathentry kind="src" path="src/main/java"/>
	<classpathentry kind="src" path="src/main/resources"/>
	<classpathentry kind="src" path="src/test/java"/>
	<classpathentry kind="src" path="src/test/resources"/>
	
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.7.0">
		<attributes>
			<attribute name="owner.project.facets" value="java"/>
		</attributes>
	</classpathentry>
	<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
	<!-- 
	<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v7.0">
		<attributes>
			<attribute name="owner.project.facets" value="jst.web"/>
		</attributes>
	</classpathentry> -->
	<classpathentry exported="true" kind="con" path="org.springsource.ide.eclipse.gradle.classpathcontainer">
		<attributes>
			<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
		</attributes>
	</classpathentry>
	<classpathentry kind="output" path="WebContent/WEB-INF/classes"/>
</classpath>

3、在工程目录下创建build.gradle文件,编辑文件添加依赖jar包:
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'

sourceCompatibility = 1.7
webAppDirName = 'WebContent'
sourceSets.main.java.srcDir 'src'

repositories {
    mavenCentral()
}

dependencies {
	compile (
		"commons-logging:commons-logging:1.1.3",
		"org.freemarker:freemarker:2.3.16",
		"ognl:ognl:3.0.6",
		"org.apache.struts:struts2-core:2.3.15.1",
		"org.apache.struts.xwork:xwork-core:2.3.15.1",
		
		"org.slf4j:slf4j-log4j12:1.7.7",
		"org.apache.struts:struts2-json-plugin:2.3.15.1",
		"org.apache.struts:struts2-spring-plugin:2.3.15.1",
		"org.apache.struts:struts2-convention-plugin:2.3.16.3",
	)
}

说明:前五个jar包是struts2必需的。
然后刷新工程的jar依赖,至此一个基于gradle的web工程创建完毕。

二、一个简单的action
1、要使用struts2,怎能不配置filter呢?打开web.xml,添加一下内容:
<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>

2、实现action类:
package com.haochen.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6798738145555362022L;

	@Override
	public String execute() throws Exception {
		System.out.println("Hello World, struts2.");
		return SUCCESS;
	}

}

3、在类路径下新建struts.xml文件,配置action:
<?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>
	<package name="test" namespace="/a" extends="struts-default">
		<action name="helloworld" class="com.haochen.action.HelloWorldAction">
			<result name="success">/pages/result.jsp</result>
		</action>
	</package>
</struts>

4、发布,访问http://localhost:8080/struts2demo/a/helloworld.action,后台打印“Hello World, struts2.”表示成功。

猜你喜欢

转载自arthur2014.iteye.com/blog/2162974