论坛首页 Java企业应用论坛

将struts2的Action变成二级分发控制器以减少对strus2标签的依赖

浏览 7541 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-04-13   最后修改:2010-04-14

struts2是MVC框架,其中org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter过滤器是前端控制器

使用Action类模拟二级分发控制器来实现减少对s2标签的依赖,这样的话对模板原型的破坏会比较小,实现方式如下


public class _Base extends ActionSupport{

private String action ="";

private String message ="";

public void setAction(String action){this.action=action;}

public String getAction(){return this.action;}


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

public String getMessage(return this.message);



public String dispatcher(){

String _action = this.getAction();


String _invokeAction ="doIndex";


if(StringUtils.isBlank(_action)){

}else{

_invokeAction = _action;

}


String _result = SUCCESS;

try{

Object obj = org.apache.commons.beanutils.MethodUtils.invokeMethod(this, _action, new Object[]{});

if(obj!=null){

_result = obj.toString();

}

}catch(Exception e){

this.setMessage(e.getMessage());

}

return _result;

}


public String doIndex(){

return SUCCESS;

}


}

 

 

 



public class Login extends _Base{


private String username="";

private String password="";


public void setUsername(String username){this.username=username;}

public String getUsername(){return this.username;}

public void setPassword(String password){this.password=password;}

public String getPassword(){return this.password;}




public String doLogin(){

 try{

  //invoke service

  return "home";

 }catch(Exception e){

 

 }

retrun INPUT;

}


public String doLogout(){

 //clear session

return SUCCESS;

}




}

 

 




struts.xml


<action name="Login" class="Login" method="dispatcher">

<result name="success" type="freemarker">/WEB-INF/content/Login.ftl</result>

<result name="input" type="freemarker">/WEB-INF/content/Login.ftl</result>

<result name="home" type="redirectAction">Home</result>

</action>

 

 

 


Login.ftl


<html><head><title></title></head><body>




<form>

<input type="hidden" name="action" value="doLogin"/><!--此隐藏域作为分发器的分发标志-->

<input type="text" name="username" value="${username}"/>

<input type="text" name="password" value="${password}"/>

<input type="submit" value="登陆"/>

</form>




</body></html>

 

 



登出的链接则产生 Login.do?action=doLogout


此思想来源于php的mvc实现,在php中实现MVC的框架一般都是有一个前端控制器如

index.php,通过传参调用不同的控制器和action,如

index.php?ctrl=Login&action=defaultView

index.php?ctrl=Login&action=doLogin

index.php?ctrl=Login&action=doLogout

 

 

 

   发表时间:2010-04-15  
看着楼主写的下划线开头的类就很不舒服,为什么不用大写字母开头呢?不解中……
0 请登录后投票
   发表时间:2010-04-15   最后修改:2010-04-15
struts2可以使用通配符或指定方法。
1、直接指定方法(不依赖配置)
login.action!method
login!method.action

2、通配符
struts-config.xml
<action name="*path" class="" method="{1}">

不明白楼主用意
0 请登录后投票
   发表时间:2010-04-15  
感觉在做无用功
0 请登录后投票
   发表时间:2010-04-15   最后修改:2010-04-15
这种配置还是有它的用处的,可以说是对减少struts标签的依赖,也可以说减少是对struts配置信息的依赖
0 请登录后投票
   发表时间:2010-04-15  
完全不明白楼主的用意,对于减少struts2标签依赖是什么含义?
0 请登录后投票
   发表时间:2010-04-15  
确实没看出来怎么减少struts2标签的依赖
0 请登录后投票
   发表时间:2010-04-15  
aoliwen521 写道
完全不明白楼主的用意,对于减少struts2标签依赖是什么含义?

就是不用<s:form>这种标签,采用原始的html。
0 请登录后投票
   发表时间:2010-04-15  
lydawen 写道
struts2可以使用通配符或指定方法。
1、直接指定方法(不依赖配置)
login.action!method
login!method.action

2、通配符
struts-config.xml
<action name="*path" class="" method="{1}">

不明白楼主用意

动态方法调用我是禁用的,Action统配我也是去掉的,因为我认为一个物理Action类声明成两个逻辑Action意义不大
如LoginAction类---->LoginIndex.action----对应execute
LoginAction类---->LoginDoLogin.action----对应doLogin
我采用的方法是用java编程取代框架的配置,脱离框架的管理。


0 请登录后投票
   发表时间:2010-04-15  
即使楼主不用REST,也不至于这样增加工作量吧,我觉得原本的Struts2就很好用了,楼主还需要仔细看文档啊。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics