【问题标题】:<s:checkboxlist> error: have seen it around 100 times now<s:checkboxlist> 错误:已经看过大约 100 次了
【发布时间】:2015-02-06 17:19:11
【问题描述】:

我知道网站上有人问过类似类型的问题。但是提交给这些的答案还没有满足我的问题。所以这是我的错误:

我的项目的初始页面: checkBoxList.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
<body>
<s:form action="resultAction">
<h4>
<s:checkboxlist label="What's your favor color" list="colors" 
   name="yourcolor" value="defaultColor" />
</h4> 

<s:submit value="submit" name="submit" />
</s:form>
</body>
</html>

我的结果页面: result.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>

<body>
<h1>Struts 2 multiple check boxes example</h1>

<h4>
Favor colors : <s:property value="yourColor"/>
</h4> 

</body>
</html>

我的操作页面: CheckBoxListAction.java

package com.vishal.common.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class CheckBoxListAction extends ActionSupport{

private List<String> colors;

private String yourcolor;

public String getYourColor() {
    return yourcolor;
}

public void setYourColor(String yourColor) {
    this.yourcolor = yourColor;
}

public CheckBoxListAction(){
    colors = new ArrayList<String>();
    colors.add("red");
    colors.add("yellow");
    colors.add("blue");
    colors.add("green");
}

public String[] getDefaultColor(){
    return new String [] {"red", "green"};
}

public List<String> getColors() {
    return colors;
}

public void setColors(List<String> colors) {
    this.colors = colors;
}

public String execute() {
    return SUCCESS;
}

public String display() {
    return NONE;
}

}

还有我的 struts.xml 页面:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
 <package name="default" namespace="/" extends="struts-default">

 <action name="checkBoxListAction" 
     class="com.vishal.common.action.CheckBoxListAction" method="display">
<result name="none">/checkBoxList.jsp</result>
 </action>

<action name="resultAction"     class="com.vishal.common.action.CheckBoxListAction">
  <result name="success">/result.jsp</result>
 </action>
</package>

</struts>

我认为我的 web.xml 页面可能没有问题。至于我的文件夹结构,这里是项目名称:StrutsCheckbox

所以请如果有人可以帮助我解决我的错误。

【问题讨论】:

    标签: jsp struts2 checkboxlist


    【解决方案1】:

    关于您的错误,您正在尝试使用在您的 xml 配置中没有映射的 URL。您需要调用checkBoxListAction 来初始化colors 列表。并且这个键应该在值堆栈中可用。要调用该操作,请使用此 URL

    http://localhost:8080/StrutsCheckbox/checkBoxListAction.action 
    

    【讨论】:

    • 不,你不需要它,它可以被重定向。就像在this 回答中一样。
    【解决方案2】:
    1. 避免使用NONE(这是一个特定的预定义结果,以防止在 HttpResponse 中写入任何输出)作为映射到某物的结果。要么使用自定义结果,要么只使用SUCCESS(来自display()或来自execute(),只要映射就无所谓);

    2. 将列表加载部分从构造函数移动到您的操作方法(将来,移动到prepare() 方法或类似方法);

    例如:

    public String execute(){
        colors = new ArrayList<String>();
        colors.add("red");
        colors.add("yellow");
        colors.add("blue");
        colors.add("green");
    
        return SUCCESS;
    }
    
    <!-- if no METHOD is specified, execute() is called --> 
    <action name="checkBoxListAction" class="com.vishal.common.action.CheckBoxListAction">
        <!-- if no NAME of result is specified, SUCCESS is assumed -->
        <result>/checkBoxList.jsp</result>
    </action>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 2023-02-20
      • 1970-01-01
      相关资源
      最近更新 更多