论坛首页 Java企业应用论坛

关于webwqrk2中form提交是点击哪个提交按钮的判定

浏览 8607 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2004-07-13  
http://wiki.opensymphony.com/display/WW/Webwork+2+HTML+form+buttons+Howto

<form action="MyAction.action">
<input type="submit" name="buttonOnePressed" value="First option"> 
<input type="submit" name="buttonTwoPressed" value="Alternative Option">
</form>


public class MyAction extends ActionSupport {

    /**
     * Action implementation
     *
     * Sets the message according to which button was pressed.
     **/
    public String execute(); {
        if (buttonOnePressed); {
            message="You pressed the first button";
        } else if (buttonTwoPressed); {
            message="You pressed the second button";
        } else {
            return ERROR;
        }
        return SUCCES;
    }

    // Input parameters
    private boolean buttonOnePressed=false;
    private boolean buttonTwoPressed=false;

    public void setButtonOnePressed(boolean value); {
        this.buttonOnePressed = value;
    }


    public void setButtonTwoPressed(boolean value); {
        this.buttonTwoPressed = value;
    }

    // Output parameters

    private String message;
    public String getMessage(); {
        return message;
    }
}


无论如何都测试不出来该文章提到的效果,检测不到是哪个按钮被点击了!?
有人测试成功过吗?或者需要什么特殊配置吗?
   发表时间:2004-07-13  
把两个input的name属性设成同一个, 在action中就判断这个name所指参数的值.
0 请登录后投票
   发表时间:2004-07-13  
引用
把两个input的name属性设成同一个, 在action中就判断这个name所指参数的值.


上面那篇文章http://wiki.opensymphony.com/display/WW/Webwork+2+HTML+form+buttons+Howto 说用Value来判断不好,我也觉得不好,因为,可能Value会改变,
比如说中文或英文
0 请登录后投票
   发表时间:2004-07-13  
我刚刚测试了下面的HTML页面
<html>
<body bgcolor="#FFFFFF" text="#000000">
<form name="form1" method="get" action="1.html">
<input type="submit" name="Submit1" value="Submit">
<input type="submit" name="Submit2" value="Submit">
</form>
</body>
</html>

当点submit1时, 提交的URL是: 1.html?Submit1=Submit
当点submit2时, 提交的URL是: 1.html?Submit2=Submit
也就是多个submit按钮,浏览器只会提交点中的那个. 页面提交后,XWORK会自动将字符串值转换为布尔值, 这是一个小trick. 不知你为什么会测试不成功. 是不是用的两个button?
0 请登录后投票
   发表时间:2004-07-13  
http://wiki.opensymphony.com/display/WW/Webwork+2+HTML+form+buttons+Howto
它这个例子是针对比较早的WebWork,对最新版本的WebWork不再适用。
它下面的Comment说的很清楚:
Comments   Hide Comments 
Note that with the final release WW2 is shipped with an OGNL version that does not convert anything existent to true and null to false as It was used on this howto.
Now it will only convert the words 'true' or 'false'. So to get this sample working just change the button types to String and check null or not null.
(posted on the WW mailing list);
 

正式发布的WebWork2中,它的类型转化是由它使用的OGNL来实现。Boolean类型转化,以前它是将存在的任意值转化为true,不存在值的即null转化为false。
但现在的WebWork,完全根据字符来转化的。如果字符的值是"true",它才转化为true,其余的都转化为fase。这时可以将按钮的类型改为"String",同过它的值是否为null来判断。
因此,MyAction代码可以更改如下:
public class MyAction extends ActionSupport { 

	/** 
	 * Action implementation 
	 * 
	 * Sets the message according to which button was pressed. 
	 **/ 
	public String execute(); { 
		if (buttonOnePressed!=null); { 
			message="You pressed the first button"; 
		} else if (buttonTwoPressed!=null); { 
			message="You pressed the second button"; 
		} else { 
			return ERROR; 
		} 
		return SUCCESS; 
	} 

	// Input parameters 
	private String buttonOnePressed= null; 
	private String buttonTwoPressed= null; 

	public void setButtonOnePressed(String value); { 
		this.buttonOnePressed = value; 
	} 


	public void setButtonTwoPressed(String value); { 
		this.buttonTwoPressed = value; 
	} 

	// Output parameters 

	private String message; 
	public String getMessage(); { 
		return message; 
	} 
}
0 请登录后投票
   发表时间:2004-07-13  
谢谢两位,看来还是看文档的时候没有仔细的去看。
确实!只有按钮的值是true or false的时候才能够赋值。

看来只能用.net 的方式,放一个名hidden,提交的时候赋值一个英文字符串,美其名曰command,
然后利用这个command进行判断了,不知道这样代码是否会好看些?
0 请登录后投票
   发表时间:2004-07-13  
该打! 我没经过测试随便发言, 忘记了ognl的类型转换规则. 再次抱歉!!
0 请登录后投票
   发表时间:2004-07-13  
引用

一个名hidden,提交的时候赋值一个英文字符串

其实也没有这个必要,jxb8901也说了
引用

当点submit1时, 提交的URL是: 1.html?Submit1=Submit
当点submit2时, 提交的URL是: 1.html?Submit2=Submit

这样,如果不是你提交的按钮,则它一定会是null。通过你提交按钮的值是否为null来判断提交哪一个按钮与true or false 判断其实是一样的,它们都很优雅。
0 请登录后投票
   发表时间:2004-07-14  
呵呵,有道理啊!
那样就避免了增加一个hidden,
但是我觉得判断null的方法 例如:
if(submitA==null){}
if(submitB==null){}
好像没有那么直观
if(isCommand("logon"){}
if(isCommand("edit")){}

哪个更加好一点?
0 请登录后投票
   发表时间:2004-07-14  
引用

if(isCommand("logon"){}
if(isCommand("edit")){}

这样确实蛮直观的,但下面的做法也不错:
public class MyAction extends ActionSupport { 

    /** 
     * Action implementation 
     * 
     * Sets the message according to which button was pressed. 
     **/ 
    public String execute(); { 
        if (buttonOnePressed); { 
            message="You pressed the first button"; 
        } else if (buttonTwoPressed); { 
            message="You pressed the second button"; 
        } else { 
            return ERROR; 
        } 
        return SUCCES; 
    } 

    // Input parameters 
    private boolean buttonOnePressed=false; 
    private boolean buttonTwoPressed=false; 

    public void setButtonOnePressed(); { 
        this.buttonOnePressed = true; 
    } 


    public void setButtonTwoPressed(); { 
        this.buttonTwoPressed = true; 
    } 

    // Output parameters 

    private String message; 
    public String getMessage(); { 
        return message; 
    } 
}
0 请登录后投票
论坛首页 Java企业应用版

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