Jsp <;s:复选框列表>;错误:我已经看过大约100次了

Jsp <;s:复选框列表>;错误:我已经看过大约100次了,jsp,struts2,checkboxlist,Jsp,Struts2,Checkboxlist,我知道网站上也有类似的问题。但是提交给这些的答案还不能解决我的问题。所以我的错误是: 我的项目的初始页: checkBoxList.jsp <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <s:form action="resultAction"> <h4> <s:checkboxlist label="Wh

我知道网站上也有类似的问题。但是提交给这些的答案还不能解决我的问题。所以我的错误是:

我的项目的初始页: 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>
<%@ 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>

我的结果页面: result.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>
<%@ 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>

Struts 2多个复选框示例
喜欢的颜色:
我的行动页面: 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;
}
package com.vishal.common.action;
导入java.util.ArrayList;
导入java.util.List;
导入com.opensymphony.xwork2.ActionSupport;
公共类CheckBoxListAction扩展了ActionSupport{
私有列表颜色;
私人色彩;
公共字符串getYourColor(){
返回你的颜色;
}
public void setYourColor(字符串yourColor){
this.yourcolor=yourcolor;
}
公共CheckBoxListAction(){
颜色=新的ArrayList();
颜色。添加(“红色”);
颜色。添加(“黄色”);
颜色。添加(“蓝色”);
颜色。添加(“绿色”);
}
公共字符串[]getDefaultColor(){
返回新字符串[]{“红色”、“绿色”};
}
公共列表getColors(){
返回颜色;
}
公共无效设置颜色(列表颜色){
这个。颜色=颜色;
}
公共字符串execute(){
回归成功;
}
公共字符串显示(){
不返回任何值;
}
}

和我的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>

/checkBoxList.jsp
/result.jsp
我认为我的web.xml页面可能没有问题。在我的文件夹结构中,项目名称为:StrutsCheckbox

所以,如果有人能帮我解决我的错误,请


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

http://localhost:8080/StrutsCheckbox/checkBoxListAction.action 
  • 避免使用
    NONE
    (这是一个特定的预定义结果,用于防止在HttpResponse中写入任何输出)作为映射到某物的结果。可以使用自定义结果,也可以只使用
    SUCCESS
    (从
    display()
    或从
    execute()
    ,只要映射了它就无所谓)

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

  • 例如:

    publicstringexecute(){
    颜色=新的ArrayList();
    颜色。添加(“红色”);
    颜色。添加(“黄色”);
    颜色。添加(“蓝色”);
    颜色。添加(“绿色”);
    回归成功;
    }
    
    
    /checkBoxList.jsp
    
    不,您不需要它,它可以重定向。我喜欢你的回答。