0

I am aware that similar type of question has been asked on the site. But the answers submitted to these haven't satisfied my problem yet.So here's my error:

enter image description here

The initial page of my project: 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>

My result page: 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>

My Action page: 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;
}

}

And my struts.xml page:

<?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>

I don't think there may be a problem with my web.xml page. And as of my Folder Structure, here it is with project name:StrutsCheckbox

So please if anyone can help me with my error.

enter image description here

Vishal Chugh
  • 15
  • 1
  • 7

2 Answers2

0

Regarding your error, you are trying to use the URL that has not a mapping in your xml configuration. You need checkBoxListAction to be invoked to initialize colors list. And this key should be available in the value stack. To invoke that action use this URL

http://localhost:8080/StrutsCheckbox/checkBoxListAction.action 
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • No, you don't need it, it could be redirected. Like in [this](http://stackoverflow.com/a/17615096/573032) answer. – Roman C Feb 09 '15 at 12:44
0
  1. Avoid using NONE (that is a specific predefined result to prevent from writing any output in the HttpResponse) as a result mapped to something. Either use a custom result, or just use SUCCESS (from display() or from execute(), it doesn't matter as long as it is mapped);

  2. Move the list loading part from the constructor to your action method (and in future, to a prepare() method or similar);

Eg:

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>
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243