Struts2 convention-plugin 使用笔记

最近有在学习struts2  ,之前虽然使用过,但是也只是使用,struts2 的很多功能根本就没去了解过,现在决定学习一下,暂不要求有多深入。所以如果您需要的是一个高级教程,那么请转车。。。此次列车不去那里。

系统环境如下

  • eclipse 3.6
  • tomcat 6
  • JDK 6
  • Ubuntu 12.04 LTS
struts 版本为2.3.4.1,在 struts2的官方文档中讲到
The Convention Plugin is bundled with Struts since 2.1 and replaces the Codebehind Plugin and Zero Config plugins.

在struts2.1 之后,已经使用Convention Plugin替换了CodeBehind,如果您使用的版本与我使用的版本不一致的话,请注意这里的区别以及由此引起的配置差异。

需要使用Jar包如下(在struts2的lib包下都有,粘贴复制就OK了)

  • asm-3.3.jar
  • asm-commons-3.3.jar
  • commons-fileupload-1.2.2.jar
  • commons-io-2.0.1.jar
  • commons-lang3-3.1.jar
  • freemarker-2.3.19.jar
  • javassist-3.11.0.GA.jar
  • ognl-3.0.5.jar
  • struts2-convention-plugin-2.3.4.1.jar
  • struts2-core-2.3.4.1.jar
  • xwork-core-2.3.4.1.jar

这些是我发现的最少包配置,至少我在删除这些包的时候,总会出现各种各样的错误的。也许你长的比较帅气(性感)  struts会卖你面子不报错的 大笑

这里要注意javassist-3.11.0.GA.jar   这个包,如果删除这个包程序会报错无法启动,因为ongl这个包依赖它,而其他的某一个包依赖ongl,所以这两个包是不能删掉的。


首先用eclipse 新建一个 动态web项目 这一步不需要多说,相信大家都会的,不会的话那么、这个、那个,你去百度一下吧.新建完成后,将以上各包复制进WEB-INF 下面的lib包下,build path。


在src目录下新建一个xml,我这里名字叫做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.convention.classes.reload" value="true" />  
	<constant name="struts.devMode" value="true" />
<!-- Convention Plugin  扫描注解时候的跟路径,根据你的项目包结构做出改变 -->
 <constant name="struts.convention.package.locators.basePackage" value="struts"/>  
<!--Convention Plugin  扫描注解的最基础包   我这里actions包存放的是项目的action 
Convention Plugin默认会去扫描所以继承了ActionSupport 和名字以Action结尾的文件并将其作为一个Action;这两个配置非必须,我发现我就算将其注释掉后程序依然可以运行。
 -->
	<constant name="struts.convention.package.locators" value="actions"/>   
</struts>


如果需要可以新建一个struts.properties文件,根据文档来讲,和struts.xml 是差不多的,
 All properties can also be set using Constant Configuration in an XML configuration file.

,这个文件提供以K-V模式配置,


在WEB-INF 下的web.xm文件我的配置如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<display-name>Struts</display-name>
	<filter>
               <!-- 注意这个filterclass  ,-->
                <filter-name>action</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
                <!-- 如果你的struts版本<2.1.3  那么请使用 这个filter                  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 -->

 <!-- 这里也是定义action所在包,我将其注释掉程序可以运行,不过如果你的包结构比较复杂,还是建议将这个配置上 <init-param><param-name>actionPackage</param-name><param-value>struts.action</param-value></init-param> --></filter><filter-mapping><filter-name>action</filter-name><url-pattern>/*</url-pattern></filter-mapping><jsp-config><taglib><taglib-uri>/s</taglib-uri><taglib-location>/WEB-INF/struts-tags.tld</taglib-location></taglib></jsp-config><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></web-app>

在web-inf 文件下新建一个  struts-tags.tld (名字随便,但是注意在web.xml中   tablib配置中的taglib-location要和你起的名字一致,路径也要一致)。打开struts-tags.tld将struts2-core-2.3.4.1.jar下META-INF 下struts-tags.tld的文件内容复制到你新建的struts-tags.tld ,当然,你也可以不用新建,直接将这个文件复制到WEB-INF下面。 之所以配置taglib,这样的JSP在使用struts标签时候,就不需要去链接远程路径了。

到此,基本工作基本完成了,这是最简单的配置,如果你有需要,那么就去按照你i的需求进行配置。配置方法参见struts的doc。

那么开始编写我们的action,我这里将其命名为IndexAction。代码很简单,只是返回一个字符串“Hello Struts”。代码如下:

package struts.actions;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;

import com.opensymphony.xwork2.ActionSupport;

@ResultPath(value="/")
public class IndexAction extends ActionSupport {

	private String message;

	public String getMessage() {
		return message;
	}

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

	/**
	 * ,
	 */
	@Action(value = "index", results = { @Result(name = SUCCESS, location = "index.jsp")})
	public String index() {
		this.setMessage("Hello Struts");
		return SUCCESS;
	}

}

代码很简单,相信大家都看得懂,这里需要注意的是一点 :@ResultPath的配置,Convention Plugin在默认情况下,会去WEB-INF/content  下查找你的result,使用@ResultPath配置,可以改变这个路径。比如在我的代码中,回去/下面查找index.jsp文件,如果将@ResultPath注释掉,你会得到一个404的错误。还有一个方式,就是你可以在result的location中这样写/index.jsp,  这样配置和代码中的配置结果是一样的。

下面就是我们的index.jsp了,同样很简单,代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--  引入struts 标签库  -->
 <%@taglib prefix="s" uri="/s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:property value="message"/>
</body>
</html>

这样就OK了,将项目部署到tomcat中,启动tomcat,打开浏览器访问 http://ip:port/appName/index.action   你会不会看到hello Struts呢? 如果不会,那么检查一下你的代码吧。。。。

在这里再说一个问题,由于在struts.xml中将devMode设置为true了,如果你改变了JAVA代码,或者其他需要重新部署的操作,都会报一个错误,不知道是为什么,暂时还没去仔细研究。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值