'struts2:getting value of multiple checkbox and made them checked on another page

I am working on struts2. I have 3 checkbox in my jsp page (say a.jsp) like

<s:checkbox name="authority" fieldValue="ORIGINATOR"/>
<s:checkbox name="authority" fieldValue="EVALUATOR"/>
<s:checkbox name="authority" fieldValue="EXECUTOR"/>

Suppose I checked first two and when I fetched the value of “authority” in my action class in gives “ORIGINATOR, EVALUATOR”. Now in another jsp page (say b.jsp) I have all these checkbox as it is and I need those two checkbox should be checked here what I have checked in my previous jsp page (a.jsp).

Thanks in advance.



Solution 1:[1]

You can set the "value" property to "true" to make this check box checked. For example, you can write the code like this: < s:checkbox name="authority" fieldValue="ORIGINATOR" value="%{var}" > while the "var" is in the server side.

Well, this is an example:

the a.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <s:form action="Handler" method="post">
        <s:checkbox name="authority" fieldValue="ORIGINATOR" label="ORIGINATOR"/>
        <s:checkbox name="authority" fieldValue="EVALUATOR" label="EVALUATOR"/>
        <s:checkbox name="authority" fieldValue="EXECUTOR" label="EXECUTOR"/>
        <s:submit label="Submit"></s:submit>
    </s:form>
</body>
</html>

b.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <s:form>
        <s:checkbox name="authority" fieldValue="ORIGINATOR" value="%{isORIGINATORSet}" label="ORIGINATOR"/>
        <s:checkbox name="authority" fieldValue="EVALUATOR" value="%{isEVALUATORSet}" label="EVALUATOR"/>
        <s:checkbox name="authority" fieldValue="EXECUTOR" value="%{isEXECUTORSet}" label="EXECUTOR"/>
    </s:form>
</body>
</html>

The handler is:

package com.sesoft.test;

import com.opensymphony.xwork2.Action;

public class Handler implements Action{

    private String isORIGINATORSet = "false";
    
    private String isEVALUATORSet = "false";
    
    private String isEXECUTORSet = "false";
    
    private String[] authority;
    
    @Override
    public String execute() throws Exception {
    
        
        for(String s : authority){
            
            if(s.equals("ORIGINATOR"))
                isORIGINATORSet = "true";
            if(s.equals("EVALUATOR"))
                isEVALUATORSet = "true";
            if(s.equals("EXECUTOR"))
                isEXECUTORSet = "true";
        }
        
        return Action.SUCCESS;
    }

    public void setAuthority(String[] authority){
        
        this.authority = authority;
    }
    
    public String getIsORIGINATORSet(){
        
        return this.isORIGINATORSet;
    }
    
    public String getIsEVALUATORSet(){
        
        return this.isEVALUATORSet;
    }
    
    public String getIsEXECUTORSet(){
        
        return this.isEXECUTORSet;
    }
}

Solution 2:[2]

convert the comma separated string to String[] and set the string array as value to checkboxlist.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Smamatti