gpt4 book ai didi

java - 如何使用jsp在按钮的单击事件上设置struts bean的属性

转载 作者:行者123 更新时间:2023-12-01 13:45:01 25 4
gpt4 key购买 nike

我是struts甚至web应用程序的新手...我想在按钮的单击事件上使用jsp设置struts form bean的属性。但我不能做同样的事情......

以下是代码:

jsp代码

function importDistList(){
alert("Importing List function called...");
var val = document.forms['myForm'].importButtonClicked.value;

var val = document.forms['myForm'].elements['importButtonClicked'].value = "true";

alert("val :: "+val);
}

<console:button name="importMembers" script="importDistList();">
<console:label><bean:message key="com.demo.web.console.importList"/></console:label>
</console:button>

我没有得到 true 值...并且没有显示警报...而是给出错误消息:'document.forms.myForm.elements.importButtonClicked' 为 null 或不是对象

表单 bean 代码:

public class MyForm extends ActionForm {
private String importButtonClicked;

public void setImportButtonClicked(String importButtonClicked){
this.importButtonClicked = importButtonClicked;
}

public String getImportButtonClicked(){
return importButtonClicked;
}

}

struts-config.xml

<form-beans>
<form-bean name="myCSVForm" type="com.demo.web.console.MyCSVForm"/>

</form-beans>
<action-mappings>
<action path="/importCSV"
type="com.demo.web.console.MyCSVAction"
scope="session"
name="myForm"
input="/myCSV.jsp">
<forward name="success" path="/myDialog.jsp" />
</action>
</action-mappings>

在上面的jsp代码中,使用了自定义标签库,其代码为:

public class ButtonTag extends BodyTagSupport {
private static Log log = LogFactory.getLog(ButtonTag.class);

/**
* The name (id) of this button. This way you can
* retrieve it from some javascript and disable
* this button.
*/
private String name;
/**
* The "onClick" javascript.
*/
private String script;

/**
* The "href" value of the link.
*/
private String href;

/**
* If the button is enabled or not.
*/
private String enabled = "enabled";

/**
* cell attributes, allows for button placement
*/
private String cellattr;

/**
* the button has a popup attached to it.
*/
private String hasPopup = "false";

/**
* the button's only purpose is to display a pop up menu
*/
private String isPopupOnly = "false";

private String buttonLabel;

private String popupId = null;


public int doAfterBody() throws JspException {
if (href == null && script == null && !isButtonAPopupOnly()) {
throw new IllegalStateException("Either href or script parameter must be provided.");
}

try {
BodyContent bc = getBodyContent();
String content = bc.getString();
String buttonClass = null;

if (isEnabled()) {
buttonClass = "button";
}
else {
buttonClass = "buttonROLL_disabled";
}

/** cause sometimes the button is by itself or in a button bar */
if ( cellattr != null )
{
getPreviousOut().print("<td ");
getPreviousOut().print( cellattr );
getPreviousOut().print(">");
}
else
{
getPreviousOut().print("<td>");
}
/* getPreviousOut().print("<table cellSpacing=\"0\" cellPadding=\"0\" border=\"0\" id=\"" + name + "_table\"><tbody><tr><td>");
getPreviousOut().print("<table class=\"blackBorder\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\"><tbody><tr>");
getPreviousOut().print("<td class=\"");
*/
getPreviousOut().print("<table cellSpacing=\"0\" cellPadding=\"0\" border=\"0\" id=\"" + name + "_table\" class=\"blackBorder\">\n<tr>\n");
getPreviousOut().print("<td id=\"" + name + "\" nowrap");
getPreviousOut().print(" class=\"");

if (isEnabled()) {
getPreviousOut().print("button");
}
else {
getPreviousOut().print("button_disabled");
}
getPreviousOut().print("\"><a href=\"");

if ( isButtonAPopupOnly() )
{
getPreviousOut().print( "#" + popupId );
}
else if (script != null && href != null) {
getPreviousOut().print(href);
getPreviousOut().print("\" onclick=\"");
getPreviousOut().print(script);
}
else if (script != null) {
getPreviousOut().print("javascript:");
getPreviousOut().print(script);
}
else if ( href != null ){
getPreviousOut().print(href);
}
else {
getPreviousOut().print("#");
}
getPreviousOut().print("\" class=\"");
if (isEnabled()) {
getPreviousOut().print("buttonRoll");
}
else {
getPreviousOut().print("buttonRoll_disabled");
}


if ( isButtonAPopupOnly() )
{
getPreviousOut().print("\" id=\"");
getPreviousOut().print(name);
getPreviousOut().print("_label\">");
getPreviousOut().print( buttonLabel );
getPreviousOut().print( "&nbsp<img src=\"images/i_sort_des.gif\" border=\"0\"/>" );
getPreviousOut().print("</a>");
getPreviousOut().print( content );
getPreviousOut().print("</td>\n");
}
else if ( buttonHasAPopup() )
{
getPreviousOut().print("\" target=\"_self\" id=\"");
getPreviousOut().print(name);
getPreviousOut().print("_label\">");
getPreviousOut().print( buttonLabel );
getPreviousOut().print("</a></td>\n");
getPreviousOut().print( "<td id=\" ");
getPreviousOut().print( name );
getPreviousOut().print("PopupButton nowrap class=\"button\"><a href=\"#" );
getPreviousOut().print( popupId );
getPreviousOut().print("'); class=\"buttonRoll\"><img src=\"images/i_sort_des.gif\" border=\"0\"></a>" );
getPreviousOut().print( content );
getPreviousOut().print( "</td>\n" );
}
else // a basic button
{
getPreviousOut().print("\" target=\"_self\" id=\"");
getPreviousOut().print(name);
getPreviousOut().print("_label\">");
getPreviousOut().print( buttonLabel );
getPreviousOut().print("</a></td>\n");
}

getPreviousOut().print("</tr></table></td>\n");
} catch (IOException e) {
e.printStackTrace();
throw new IllegalStateException("Error creating the page's HTML code.");
}

return SKIP_BODY;
}

public int doEndTag() throws JspException {
return EVAL_PAGE;
}

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

public String getName() {
return name;
}

public String getPopupButtonName() {
if ( isButtonAPopupOnly() )
return name;
else
return name + "PopupButton";
}

public void setScript(String script) {
this.script = script;
}

public String getScript() {
return script;
}

public void setHref(String href) {
this.href = href;
}

public String getHref() {
return href;
}

public void setCellattr(String cellattr) {
this.cellattr = cellattr;
}

public String getCellattr() {
return cellattr;
}

public void setButtonEnabled(String enabled) {
this.enabled = enabled;
}

public String getButtonEnabled() {
return enabled;
}

public boolean isEnabled() {
return enabled.equals("enabled");
}

public void setIsPopupOnly(String isPopupOnly) {
this.isPopupOnly = isPopupOnly;
}

public String getIsPopupOnly() {
return isPopupOnly;
}

public boolean isButtonAPopupOnly() {
return isPopupOnly.equals("true");
}

public void setHasPopup(String hasPopup) {
this.hasPopup = hasPopup;
}

public String getHasPopup() {
return hasPopup;
}

public boolean buttonHasAPopup() {
return hasPopup.equals("true");
}

public void setButtonLabel(String buttonLabel) {
this.buttonLabel = buttonLabel;
}

public String getButtonLabel() {
return buttonLabel;
}

public void setPopupId(String popupId) {
if ( this.popupId == null)
this.popupId = popupId;
}

public String getPopupId() {
return popupId;
}
}

最佳答案

  1. 在操作映射中,name="myForm"应该与 name="myCSVForm" 相同在 form-bean
  2. 属性名称应与表单元素实体的名称相同
  3. 提交表单或使用 (JSTL) 时会自动设置属性值 <c:set> (不能使用JS)
  4. 使用 <bean:write> 获取值

关于java - 如何使用jsp在按钮的单击事件上设置struts bean的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20421214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com