Struts2: get the value of multiple checkboxes and validate them on another page

I am working on struts2. I have 3 checkboxes 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 the first two, and when I get the "authority" value in my action class, it returns "ORIGINATOR, EVALUATOR". Now in another jsp page (say b.jsp) I have all these checkboxes as they are, and I need these two checkboxes to be checked here, which I checked in my previous jsp page (a.jsp).

Thanks in advance.

+1


a source to share


2 answers


You can set the value property to "true" for this checkbox to be checked. Got it? For example, you can write code like this: <s: checkbox name = "authority" fieldValue = "ORIGINATOR" value = "% {var}"> while "var" is on the server side.

Well this is an example:

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>

      

Handler:

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;
    }
}

      

+4


a source


convert comma separated string

to String[]

and set string array as value checkboxlist

.



+3


a source







All Articles