Jump to content

JSP + Servlet getParameterValues


ricardo_1977

Recommended Posts

Boas,

eu tenho uma pagina jsp com uma select list onde quero que seja possivel fazer mais que uma selecao.

depois vou num servlet quero receber os valores seleccionados.

o problema é que so me devolve o ultimo valor seleccionado.

aqui vai parte do codigo

codigo jsp:

<form action = "myservlet" method = "post">
....
<select   name ="selectPaises"  style="border:1px solid #58ACFA">
				 <option value ="" ><center></center></option>
				    <option value="EU">Europa comunitaria</option>
				    <option value ="TE">Toda a Europa</option>
				 <%
			    if(paises!=null) {
				   Set<String>  aux = paises.keySet();

			    for (String p: aux) {
			    %>
			    <option  value="<%=p%>">
					    <%=p%>
			    </option>
			    <%

										   }
								   }
			    %>
			    </select>
</form>
....

codigo servlet:

....
String[] listaPaises =  request.getParameterValues("selectPaises");
.....
Link to comment
Share on other sites

Se a tua página está em HTML 4.01 tens de por multiple no select.

<select name="selectPaises" style="whatever" multiple>
<option>...
...
</select>

What have you tried?

Não respondo a dúvidas por PM

A minha bola de cristal está para compor; deve ficar pronta para a semana.

Torna os teus tópicos mais atractivos e legíveis usando a tag CODE para colorir o código!

Link to comment
Share on other sites

Ah! O que queres então é uma série de checkboxes ...

<label><input type="checkbox" name="selectPaises[EU]">Europa Comunitaria</label>
<label><input type="checkbox" name="selectPaises[TE]">Toda a Europa</label>
...

Edit ... dentro dum div com altura fixa e que "scrolla"

Edit2: se calhar tens que acertar o que está dentro das [] no nome das checkboxes para ficar compativel com Java

Edited by pmg

What have you tried?

Não respondo a dúvidas por PM

A minha bola de cristal está para compor; deve ficar pronta para a semana.

Torna os teus tópicos mais atractivos e legíveis usando a tag CODE para colorir o código!

Link to comment
Share on other sites

Ah! O que queres então é uma série de checkboxes ...

<label><input type="checkbox" name="selectPaises[EU]">Europa Comunitaria</label>
<label><input type="checkbox" name="selectPaises[TE]">Toda a Europa</label>
...

Edit ... dentro dum div com altura fixa e que "scrolla"

Não, eu quero um select em que o utilizador possa escolher mais que uma opcao.

uma especie de um select com uma lista de hobbies:

cinema

desporto

PC

etc

e onde o utilizador possa escolher uma de que uma opcao.

o problema é que o servlet so recolhe a ultima opcao seleccionada no select

Link to comment
Share on other sites

Se a tua página está em HTML 4.01 tens de por multiple no select.

<select name="selectPaises" style="whatever" multiple>
<option>...
...
</select>

A forma correcta é com multiple="multiple".

Penso que o ideal é utilizares uma das componentes desenvolvidas com JQuery UI, por exemplo:

http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Link to comment
Share on other sites

A forma correcta é com multiple="multiple".

Talvez seja em XHTML ou XML ou HTML 1.042 ...

Em HTML 4.01, como especificado no link submetido, a forma correcta é como eu coloquei (os browsers devem, alem de aceitar a forma correcta, aceitar a forma incorrecta que tu dizes ser correcta).

What have you tried?

Não respondo a dúvidas por PM

A minha bola de cristal está para compor; deve ficar pronta para a semana.

Torna os teus tópicos mais atractivos e legíveis usando a tag CODE para colorir o código!

Link to comment
Share on other sites

Pois, mas o multiple="multiple" é mais portável, pelo que deve ser considerada a abordagem correcta, independentemente da forma como os browsers lidam com a questão.

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Link to comment
Share on other sites

Tambem o ingles e mais portavel que o portugues, pelo que toda a gente deve usar "mouse" em vez de "rato" independentemente de quem esta a ouvir 🙂

What have you tried?

Não respondo a dúvidas por PM

A minha bola de cristal está para compor; deve ficar pronta para a semana.

Torna os teus tópicos mais atractivos e legíveis usando a tag CODE para colorir o código!

Link to comment
Share on other sites

Em HTML 5, com o DOCTYPE "<!DOCTYPE html>" a sintaxe do <select> é a mesma do que para HTML 4.01 ("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">")

http://www.w3.org/TR/2012/WD-html5-20120329/the-select-element.html#the-select-element

Eu acho estranho alguem fazer, por exemplo, uma pagina assim:

<!DOCTYPE html>
<html>
<head><title>hello world</title></head>
<body><p>Hello<br/>world</p></body>
</html>

Aquele <br/>, a meu ver, esta completamente deslocado

Edited by pmg

What have you tried?

Não respondo a dúvidas por PM

A minha bola de cristal está para compor; deve ficar pronta para a semana.

Torna os teus tópicos mais atractivos e legíveis usando a tag CODE para colorir o código!

Link to comment
Share on other sites

Mas aqui não é exactamente uma questão de linguagens diferentes. Em português podes dizer "fila" ou "bicha", o que não quer dizer que ambas sejam interpretadas por toda a gente da mesma forma. 🙂

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Link to comment
Share on other sites

Mas o que é que não funciona? A selecção múltipla, ou a recepção dos itens seleccionados?

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Link to comment
Share on other sites

Chegaste a ver aquilo que coloquei anteriormente?

http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Link to comment
Share on other sites

Não irá ser exactamente uma solução, porque a solução já aqui foi dada. O problema é que tu não queres o comportamento comum do select, daí que precisas efectivamente de utilizar uma componente que implemente o comportamento que tu queres (ou implementares tu a tua própria componente). Existem centenas e funcionam perfeitamente bem.

Lembra-te que, com checkboxes, a implementação é radicalmente diferente da implementação para o select, pelo que aquilo que estás a desenvolver agora para as checkboxes não deverá ser directamente compatível com a versão com o select. Quando fores fazer a alteração tens que ter isso em conta.

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.