Doubt with checkbox value true and checked

0

I have a form and to update I have this code snippet. I can not understand what he means. Type, if the student is not active on the system it places it as value="true"? Otherwise it also places as value="true" but checked="checked". What does that mean?

    <%if(aluno.getAtivo() == false){ %>
        <input type="checkbox" id="ativo" name="ativo" value="true"/><br>
    <%}else { %>
        <input type="checkbox" id="ativo" name="ativo" value="true" checked="checked"/><br>
    <%} %>      
    <input type="submit" value="Atualizar"> 

Follow the full jsp page:

<%@page import="DAO.AlunoDAO"%>
<%@page import="MODEL.Aluno"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Atualizando aluno</title>
</head>
<body>
<% Aluno aluno = new AlunoDAO().find(Integer.parseInt(request.getParameter("id")));%>

<form action="alunoCONTROLLER" method="get">
    <input type="hidden" name="operacao" value="atualizar">
    <input type="hidden" name="id" value="<%=aluno.getId()%>">

    <label for="name">Nome:</label>
    <input type="text" id="nome" name="nome" value="<%=aluno.getNome()%>"/><br/>

    <label for="telefone">Telefone:</label>
    <input type="text" id="telefone" name="telefone" value="<%=aluno.getTelefone() %>"/><br/>

    <label for="email">E-mail:</label>
    <input type="text" id="email" name="email" value="<%=aluno.getEmail()%>" /><br>

    <label for="endereco">Endereço:</label>
    <input type="text" id="endereco" name="endereco" value="<%=aluno.getEndereco() %>"/><br>

    <label for="matricula">Matricula</label>
    <input type="text" id="matricula" name="matricula" value="<%=aluno.getMatricula() %>"/><br>

    <label for="idade">Idade</label>
    <input type="text" id="idade" name="idade" value="<%=aluno.getIdade() %>"/><br>

    <label for="ativo">Ativo:</label>

    <%if(aluno.getAtivo() == false){ %>
        <input type="checkbox" id="ativo" name="ativo" value="true"/><br>
    <%}else { %>
        <input type="checkbox" id="ativo" name="ativo" value="true" checked="checked"/><br>
    <%} %>      
    <input type="submit" value="Atualizar">     
</form>

 

    
asked by anonymous 15.02.2016 / 22:08

1 answer

2

Your question is related to HTML, not JSP.

Do not confuse value = true with checked.

Although it may be confusing, the value attribute is not setting whether or not the checkbox is checked in the UI. This attribute is used to define what will be received on the server when an HTTP POST request is made (obviously the server only receives if the checkbox is checked).

The attribute that controls the state of the checkbox in the user interface is checked. There are two ways to use it:

<input type="checkbox" id="ativo" name="ativo" value="true" checked="checked"/>

or

<input type="checkbox" id="ativo" name="ativo" value="true" checked/>

So the code you posted is correct. When aluno.getAtivo() == true , you render a checkbox with the checked and value=true attribute. When aluno.getAtivo() == false , you render the checkbox without the checked attribute, but with value also equal true . This is done because if the user clicks on the checkbox and sends, true will be received on the server, which is consistent with your problem.

See another example below for a better understanding.

<form action="form_demonstracao" method="get">
  <input type="checkbox" name="veiculo" value="Bicicleta"> Eu tenho uma bicicleta<br>
  <input type="checkbox" name="veiculo" value="Carro" checked> Eu tenho um carro<br>
  <input type="submit" value="Submit">
</form>

If the form above is submitted the way it is, the server will receive the following:

veiculo=Carro

If the bicycle checkbox is checked, then the following will be received:

veiculo=Carro&veiculo=Bicicleta
    
15.02.2016 / 23:26