Struts2注解开发之Configuration by Convention(一)

        随着struts2的不断升级,Struts开始使用convention-plugin代替codebehind-plugin来实现struts的零配置。所谓的零配置并不是任何配置都不需要,而是采用约定大于配置的方式。

在web开发过程中,根据convention-plugin的默认约定,我们不再需要在Struts.xml中配置任何信息。

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

struts.xml
[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
  5. <struts>  
  6.     <package name="default" namespace="/" extends="struts-default" />  
  7. </struts>  
我的action

package com.redhamr.web.member.actions;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.redhamr.comp.member.MemberHandler;
import com.redhamr.persistent.model.Member;
import com.redhamr.web.member.GlobalConstant;

/**
 * 商户信息管理
 * 
 * @author zonto
 */
@Scope("prototype") 
@Controller("loginact") 
@ParentPackage("index")
public class LoginAct extends BasePageAct{
	private static final long serialVersionUID = 1L;
	
	@Autowired MemberHandler memberhandler;
	
	private Member member;

	private String msg;
	private String gopath=SUCCESS;
	private String referrer;//推荐人


	/**
	 * 会员登录
	 * @return
	 */
	@Action(value = "memberlogin",results = {
			@Result(name = "success",type="chain",location="memlist"),
			@Result(name = "input",location="/login.jsp")}) 
	public String merchOperLogin(){
		
		if(member==null){
			this.setMsg("请输入您的帐号和登陆密码!");
			return INPUT;
		}
		
		try {
			member=memberhandler.memLogin(member.getUsername(),member.getPassword());
			if(member==null){
				this.setMsg("帐号和登陆密码错误!");
				return INPUT;
			}
		} catch (Exception e) {
			this.setMsg("帐号和登陆密码错误!");
			return INPUT;
		}
		ActionContext.getContext().getSession().put(GlobalConstant.SESSION_MEMBER,member);
		
		this.setMsg("登陆成功!");
		return this.getGopath();
	}
}

我的页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<s:property value="msg" escape="false"/>
<s:form action="memberlogin">
用户名:<s:textfield name="member.username"/>
密   码:<s:password name="member.password"/>
<s:submit/>
</s:form>


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

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

  • Action location by package naming conventions
    com.example. action.Main Action
    com.example. actions.products.Display (implements com.opensymphony.xwork2.Action)
    com.example. struts.company.details.ShowCompanyDetailsAction
    com.example. struts2.company.details.ShowCompanyDetailsAction

          Convention plugin默认将包路径包含action,actions,struts,struts2的所有包都作为Action类的路径来搜索。你可以通过设置struts.convention.package.locators属性来修改这个配置。Convention plugin默认的配置如下:

[html]  view plain copy
  1. <constant name="struts.convention.package.locators" value="action,actions,struts,struts2"/>  

Convention plugin在上述的包及其子包中查找后缀为Action、或者实现了com.opensymphony.xwork2.Action的类。你可以通过设置

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

[html]  view plain copy
  1. <constant name="struts.convention.action.suffix" value="Action"/>  

  • Result (JSP, FreeMarker, etc) location by naming conventions

          当在浏览器中输入http://localhost:8080/redhamr_web_member/login.jsp访问时,页面会跳转到WEB-INF/content/login.jsp。
          默认情况下, Convention plugin假设所有的results存放在WEB-INF/content/下。可以通过设置struts.convention.result.path进行修改。Convention plugin默认的配置如下:

[html]  view plain copy
  1. <constant name="struts.convention.result.path" value="/WEB-INF/content/"/>  

          Convention plugin即使在找不到对应的action的情况下,也可以通过Action的URL(这里是/hello-world)找到对应的结果(/hello-world.jsp)。

  • Class name to URL naming convention
          com.example.actions.MainAction -> /main
          com.example.actions.products.Display -> /products/display

          com.example.struts.company.details.ShowCompanyDetailsAction -> /company/details/show-company-details

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


  • Package name to namespace convention
          com.example.actions.MainAction -> /
          com.example.actions.products.Display -> /products

          com.example.struts.company.details.ShowCompanyDetailsAction -> /company/details

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


              正是有了这些“约定”才使得我们的LOGIN能够实现零配置。

          实际的开发过程中如果只这些约定,也许不能完全满足实际项目的需要。幸运的是Convention Plugin的配置是相当灵活的。


 总结 :使用注解来配置Action的最大好处就是可以实现零配置,但是事务都是有利有弊的,使用方便,维护起来就没那么方便了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值