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

发布于 2024-07-19 08:35:27 字数 425 浏览 1 评论 0原文

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

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

假设我检查了前两个,当我在操作类中获取“authority”的值时给出“ORIGINATOR,EVALUATOR”。 现在,在另一个 jsp 页面(例如 b.jsp)中,我按原样拥有所有这些复选框,并且我需要在此处检查我在之前的 jsp 页面(a.jsp)中检查过的这两个复选框。

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感性 2024-07-26 08:35:27

您可以将“value”属性设置为“true”以选中此复选框。
例如,您可以编写这样的代码: < s:checkbox name="authority" fieldValue="ORIGINATOR" value="%{var}" > 而“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;
    }
}

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;
    }
}
木緿 2024-07-26 08:35:27

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文