`

struts2零配置之helloworld

 
阅读更多

 

从 Struts 2.1 开始,Struts 可以使用 Convention 插件来支持零配置:Convention 插件完全抛弃配置信息,不仅不需要使用 struts.xml 文件进行配置,甚至不需要使用 Annotation 进行配置。而是完全根据约定来自动配置。

 

首先,让我们先来看一下基于convention-plugin实现的Struts"零配置"的HelloWorld。

 

代码结构:

 


 

实现步骤:

 

1. 新建Dynamic Web工程 org.rabbitx.web.struts2.convention;

2. 把struts-2.3.15.3-all包中示例目录apps下的struts2-blank.war项目解压并把其lib下的所有jar包拷贝到新建工程的/WEB-INF/lib目录下;

3. 把struts-2.3.15.3\lib\struts2-convention-plugin-2.3.15.3.jar拷贝到新建工程的/WEB-INF/lib目录下;

4. 在/WEB-INF目录下添加web.xml文件;

    

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

<web-app 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_3_0.xsd"
	version="3.0">
	<display-name>Struts2-Convention</display-name>
	
	<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>/*</url-pattern>
	</filter-mapping>

	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

5. 在src目录下添加struts.xml文件(可以把struts2-blank.war项目中的struts.xml拷贝过来进行修改); 

     

<?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.devMode" value="true" />
</struts>

 

6. 在src目录下添加日志配置文件log4j.properties(此步骤非必须,主要是为了打印日志);

     

log4j.rootLogger=info,stdout,D,E

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

log4j.appender.D=org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File=logs/log.log
log4j.appender.D.Append=true
log4j.appender.D.Threshold=DEBUG
log4j.appender.D.layout=org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}  [%t:%r] - [%p]  %m%n

log4j.appender.E=org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File=logs/error.log
log4j.appender.E.Append=true
log4j.appender.E.Threshold=ERROR
log4j.appender.E.layout=org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} 

org.apache.struts2=DEBUG
com.opensymphony.xwork2=DEBUG

 

7. 在src目录下新建package:org.rabbitx.web.struts2.convention.helloworld;

8. 在新建的package下添加Action类; 

     

package org.rabbitx.web.struts2.convention.helloworld;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionSupport;

public class HelloworldAction extends ActionSupport {

	private static final long serialVersionUID = 2696798424098149935L;

	private Logger logger = Logger.getLogger(HelloworldAction.class);
	
	@Override
	public String execute() throws Exception {
		logger.info("------HelloworldAction-----execute-------");
		if(System.currentTimeMillis()%2 != 0)
		{
			logger.info("dispatcher to page: helloworld-error.jsp");
			return ERROR;
		}
		else
		{
			logger.info("dispatcher to page: helloworld.jsp or helloworld-success.jsp");
			return SUCCESS;
		}
	}
	
}

 

9. 在WEB-INF目录下添加目录content/convention/helloworld/;

10. 在目录WEB-INF/content/convention/helloworld/下添加jsp页面helloworld.jsp,helloworld-success.jsp和helloworld-error.jsp;

       

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.*" %>  
<%@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>Struts2-Convention</title>
</head>
<body>
    
    <h1>helloworld</h1>
    <hr/>
    <s:debug></s:debug>

</body>
</html>

    

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.*" %>  
<%@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>Struts2-Convention</title>
</head>
<body>
    
    <h1>helloworld-success</h1>
    <hr/>
    <s:debug></s:debug>

</body>
</html>

    

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.*" %>  
<%@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>Struts2-Convention</title>
</head>
<body>
    
    <h1>helloworld-error</h1>
    <hr/>
    <s:debug></s:debug>

</body>
</html>

 

11. 在eclipse中部署此应用并启动服务器;

12. 在浏览器中输入地址:http://localhost:8080/org.rabbitx.web.struts2.convention/convention/helloworld/helloworld.action;

13. 运行结果:刷新界面后,会随机显示helloworld-error或helloworld-success;

 

对于一个简单的helloWorld来讲,struts.xml里面的配置简直精简到了极致。不需要再写一堆一堆的<action>配置了。而我们需要做的只是加入一个struts2-convention-plugin-x.x.x.x.jar。

之所以在helloWorld中不需要任何配置,是因为struts2-convention-plugin进行了以下一系列的约定。 

 

[1] Action搜索范围约定:

    Convention plugin默认将包路径包含action,actions,struts,struts2的所有包都作为Action类的路径来搜索。

    可以通过设置struts.convention.package.locators属性来修改这个配置。Convention plugin默认的配置如下:

    <constant name="struts.convention.package.locators" value="action,actions,struts,struts2"/>

[2] Action认定约定:

     Convention plugin在上述的包及其子包中查找后缀为Action、或者实现了com.opensymphony.xwork2.Action的类,并把它当成Action处理。

     可以通过设置struts.convention.action.suffix属性来修改这个配置。Convention plugin默认的配置如下:

     <constant name="struts.convention.action.suffix" value="Action"/>   

[3] 结果资源约定:

     默认情况下, Convention plugin假设所有的results存放在WEB-INF/content/下。

     可以通过设置struts.convention.result.path进行修改。Convention plugin默认的配置如下:

     <constant name="struts.convention.result.path" value="/WEB-INF/content/"/>

      定位资源的约定是: actionUrl + resultCode suffix. 当某个逻辑视图找不到对应的视图资源时, Conversion 会自动试图使用 actionUrl 作为物理资源.即在找不到对应的action的情况下,也可以通过Action的URL(这里是/helloworld)找到对应的结果(/helloworld.jsp)。  

[4] 类名和Action URL之间的约定:

     Convention plugin会将com.example.struts.company.details.ShowCompanyDetailsAction 的struts后的子包转换成命名空间“/company/details/”,然后ShowCompanyDetailsAction中的“Action”后缀去掉,并将驼峰式的命名大写变小写,并在之间加上“-”(当然“-”也是Convention plugin默认的)最终形成/company/details/show-company-details。

[5] 包名和命名空间之间的约定:

      Convention plugin默认将actions、action、struts、struts2作为root Package,那么com.example.actions.MainAction的命名空间即为“/”,而com.example.actions.products.Display的命名空间为“/products”。 

 

  • 大小: 24 KB
分享到:
评论

相关推荐

    struts零配置HelloWorld-Annotation

    struts2 annotion helloworld 源代码,适合初学者看看,不懂得看看文档就知道了。

    Struts2 chm文档

    2.truts 2权威指南——第2章 Struts 2下的HelloWorld.doc 3.Struts 2权威指南——第3章 Struts 2基础.doc 4.Struts2.0系列(1-15) 5.Struts2中用Spring实现IoC.doc 6.Struts2中的零配置与CoC(Convention over ...

    Struts2.0中文教程权威版

    Struts 2权威指南——第2章 Struts 2下的HelloWorld.doc Struts 2权威指南——第3章 Struts 2基础.doc 01 为Struts 2.0做好准备 02 常用的Struts 2.0的标志(Tag) 03 Struts 2.0的Action讲解 04 在Struts 2.0中国...

    struts2Demo

    struts2演示 &lt;br&gt;1./helloworld - helloworld 2./spring - 与spring整合 3./coc - 惯例优先配置,零配置文件 - codebehind不支持redirect,chain等操作,需要自己写jsp跳转 4./crud - CRUD,Create Read...

    struts2注解详细说明

    从struts2.1版本开始,Convention Plugin作为替换替换Codebehind Plugin来实现Struts2的零配置。• 包命名习惯来指定Action位置• 命名习惯制定结果(支持JSP,FreeMarker等)路径• 类名到URL的约定转换• 包名...

    flex 4 学习资料

    基于flex4技术从零开发flex博客系统 : 1 开发环境配置与hello world(1) - 豆豆网 flex+blazeDs与Ext+dwr比较_蓝色幻想_百度空间 基于blazeDS的flex4与spring的程序实例步骤 - dreamming_now的专栏 - CSDN博客 在...

    freemarker总结

    2,使用+运算符时,如果一边是数字,一边是字符串,就会自动将数字转换为字符串再连接,如:${3 + "5"},结果是:35 使用内建的int函数可对数值取整,如: ${ (x/2)?int } ${ 1.1?int } ${ 1.999?int } ${ -1.1?int } ...

    JAVA上百实例源码以及开源项目

     Java访问权限控制,为Java操作文件、写入文件分配合适的权限,定义写到文件的信息、定义文件,输出到c:/hello.txt、写信息到文件、关闭输出流。 Java绘制图片火焰效果 1个目标文件 摘要:Java源码,图形操作,火焰...

    JAVA上百实例源码以及开源项目源代码

     Java访问权限控制,为Java操作文件、写入文件分配合适的权限,定义写到文件的信息、定义文件,输出到c:/hello.txt、写信息到文件、关闭输出流。 Java绘制图片火焰效果 1个目标文件 摘要:Java源码,图形操作,火焰...

Global site tag (gtag.js) - Google Analytics