Webwork的Action Mapping相比Struts的过人之处

来源:互联网 发布:qq游戏网络断了要等 编辑:程序博客网 时间:2024/05/08 05:37

如果要完成一个对象的CRUD操作,在struts中有两种办法

1.为每一个操作都建立一个action和路径映射

2.使用dispatchAction,在struts-config.xml中使用param参数进区分,调用不同的方法

这两种办法有一个共同之处,就是有多少个操作,就需要在struts-config.xml中配置多少个action

但在webwork中,只需要一个action path就足够了,因为他有一种特有的调用方式

这是xwork.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "xwork-1.0.dtd" >
<xwork>
  
<include file="webwork-default.xml"></include>
  
<package name="default" extends="webwork-default">
     
<default-interceptor-ref name="completeStack"></default-interceptor-ref>

  
<action name="hellopeople" class="ch2.example1.HelloPeople">
    
<result name="success">/ch2/nameresult.jsp</result>
    
<result name="input">/ch2/name.jsp</result>
  
</action>
  
</package>
</xwork>

 这是HelloPeople的代码:

 

package ch2.example1;

import java.util.Map;

import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionSupport;

public class HelloPeople extends ActionSupport implements Action{
    
private String message="";
    
private String name="";

    
    
public String getName() {
        
return name;
    }

    
public void setName(String name) {
        
this.name = name;
    }

    
public String execute() throws Exception {
        
if(name==null||name.equals("")||name.equals("world")){
            message
="please input name";
            addFieldError(
"name","blank name");
    
            
return INPUT;
        }

        
else{
        message
="hello"+name;
        getSession().put(
"1""123456");

        
return SUCCESS;
        }

    }

    
public String getMessage() {
        
return message;
    }

    
    
public Map getSession() {
        
return ActionContext.getContext().getSession();
    }

    
    
public String searchpeople(){
        
if(name==null||name.equals("")||name.equals("world")){
            message
="please input name";
            addFieldError(
"name","blank name");
    
            
return INPUT;
        }

        
else{
        message
="hello"+name+"welcome";
        getSession().put(
"1""123456");

        
return SUCCESS;
        }

    }

    
    
   
}


execute是默认的执行方法,但是,如果我们要执行searchpeople要怎样做的,肯定是不能用一个不同的action path,因为在xwork中并没有为这个方法配置单独的path

webwork用一种表达式的方式实现了对action中函数的调用

请看jsp

 

<ww:form action="hellopeople!searchpeople.action">
 
<table border="1">
    
 
<tr>
       
<td colspan="2"><ww:textfield label="请输入名称:" name="name"/></td>
     
</tr>
     
<tr>
       
<td colspan="2"><input name="submit" type="submit" value="submit"/></td>
     
</tr>
   
</table>
</ww:form>

使用<ww:form action="hellopeople!searchpeople.action">就可以调用在xwork中配置的path--hellopeople中的searchpeople方法了
原创粉丝点击