struts2:获取多个复选框的值,并在另一个页面上检查它们

时间:2009-05-18 03:23:01

标签: struts2

我正在研究struts2。我的jsp页面中有3个复选框(例如a.jsp),如

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

假设我检查了前两个,当我在我的动作类中获取“权限”的值时,给出了“ORIGINATOR,EVALUATOR”。现在在另一个jsp页面(比如b.jsp)我有所有这些复选框,我需要这两个复选框应该在这里检查我在之前的jsp页面(a.jsp)中检查了什么。

提前致谢。

2 个答案:

答案 0 :(得分:4)

您可以将“value”属性设置为“true”以选中此复选框。懂了吗? 例如,您可以编写如下代码:&lt; s:复选框名称=“权限”fieldValue =“ORIGINATOR”value =“%{var}”&gt;而“var”在服务器端。

嗯,这是一个例子:

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>

处理程序是:

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

答案 1 :(得分:3)

将逗号分隔string转换为String[],并将字符串数组设置为值checkboxlist