• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RadioButton with List Struts 2

 
sol loc
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
i have a problem with struts 2 to use a radiobutton, if someone help me to realize a page in jsp with a table and inside there is an iterator a List to see a property and in the last colonn there is a radio button. what do you do to save the value cheked??
I write the code in the jsp

<s:iterator value="utentiRicerca" var="utente" status="rowstatus">
<tr class="dispari">
<td align="center"><s:property value="#utente.matricola"></s:property></td>
<td><s:property value="#utente.cognome" ></s:property></td>
<td><s:property value="#utente.nome"></s:property></td>
<td><s:property value="#utente.stampaRuoli"></s:property></td>

<td align="center">

<s:radio list="utentiRicerca" listKey="matricola"></s:radio>
</td>

</tr>
</s:iterator>

but in the jsp, in the colon of radio butto i see the object's value and i don't want this and i want to save the value in the action. how to realise this??
someonte can to help me




thanks ....
 
Richard Golebiowski
Ranch Hand
Posts: 213
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key is what is returned and the value is what is displayed. You still need another field specified in "name" for the returned value to be set.
So you need something like this:



to make it work.
Replace "???1" with the field name for the displayed values which will be the labels for the radio buttons.

Replace "???2" with the name of the field to set to the selected value.
 
sol loc
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
thanks i try to change my jsp and i use tag display with this object List and i replace the s:radio with html input radio.
You suggest me a good link to improve strtuts 2, i use it the first time and i don't found more example.
Another questrion, in the my proget how i can use the session?? i have jsp with button to go back and i must to see the select element how to do this???

thanks your reply

solage
 
Richard Golebiowski
Ranch Hand
Posts: 213
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get the returned value for the selected radio button you set the value attribute to name of the field on the action you want to receive the value. All of your radio buttons in your group of buttons will have the value set to the same field.

You asked that you want to use the session. What do you want to do with the session?
 
sol loc
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a jsp with radio button :

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://displaytag.sf.net" prefix="display" %>

<html>
<head>
<title>Ricerca Utente</title>
</head>
<body>

<meta name="immagine" content="<%= request.getContextPath() %>/img/logoSub4.jpg"/>

<div class="boxTable">
<s:form action="utente">

<s:actionerror/>
<fieldset class="form1">

<legend><strong>Cerca utente</strong></legend>

<s:label class="1200" for="matricola" key="ricerca.matricola"/>
<s:textfield name="matricola"/>
<br/>

<s:label class="1200" for="cognome" key="ricerca.cognome"/>
<s:textfield name="cognome"/>
<br/>

</fieldset>


<div class="tableButton3">
<s:submit action="cerca" key="default.pulsanteCerca"/>
</div>
</s:form>
</div>


<br/>
<s:if test="utentiRicerca.size > 0">
<s:form action="utente">
<display:table name="utentiRicerca" id="utente" class="dati testo11" summary="testo" style="width: 100%;">
<display:caption title="Tabella degli utenti"/>
<display:column property="matricola" title="Matricola" scope="col" style="text-align: center;"/>
<display:column property="cognome" title="Cognome"/>
<display:column property="nome" title="Nome"/>
<display:column property="stampaRuoli" title="Ruoli"/>
<display:column class="w1" style="text-align: center;" title="Seleziona">
<input type="radio" name="idUtenteSelezionato" value='<s:property value="#attr.utente.id"/>'/>
</display:column>
</display:table>

<div class="tableButton3">
<s:submit action="associaRuoli" key="default.pulsanteAssociaRuoli"/>

</div>
</s:form>
</s:if>

</body>
</html>






and when i select the radio i go in antother new.jsp and in this new jsp i hava a possibility i go to back in the previous jsp and i see the same form whit the same value.
how to do this???
 
sol loc
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another questions : it is possible to do a riderect used validationMetho that it keeps things?? i try type chain than it don't forward on the jsp.
 
Richard Golebiowski
Ranch Hand
Posts: 213
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

and when i select the radio i go in antother new.jsp and in this new jsp i hava a possibility i go to back in the previous jsp and i see the same form whit the same value.
how to do this???


Save the value from previous jsp to the session in your action and then read it from the session when you go back.

 
Richard Golebiowski
Ranch Hand
Posts: 213
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another questions : it is possible to do a riderect used validationMetho that it keeps things?? i try type chain than it don't forward on the jsp.


I need some clarification on your question. Are you saying that you are not getting forwarded to the jsp or that values aren't getting forwarded to the jsp?
 
sol loc
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package it.consip.cafe.sicurezza.webapp.action;

/**
* Created by IntelliJ IDEA.
* User: hp
* Date: 21-lug-2010
* Time: 16.44.50
* To change this template use File | Settings | File Templates.
*/

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.interceptor.annotations.After;
import it.consip.cafe.common.webapp.action.AbstractAction;
import it.consip.cafe.security.business.manager.RuoloManager;
import it.consip.cafe.security.business.manager.UtenteManager;
import it.consip.cafe.security.business.model.Ruolo;
import it.consip.cafe.security.business.model.Utente;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

import static org.apache.struts2.ServletActionContext.*;

@Results({
@Result(location = "/WEB-INF/jsp/sicurezza/ricercaUtente.jsp"),


@Result(name = "associaRuoli", location = "/WEB-INF/jsp/sicurezza/associaRuoli.jsp")
})

@Namespace("/sicurezza")
public class UtenteAction extends AbstractAction {


@Autowired
UtenteManager utenteManager;

@Autowired
RuoloManager ruoloManager;

private final String ASSOCIARUOLI = "associaRuoli";
private final String INSERISCI = "inserisci";
private String matricola;
private String cognome;

private List<Utente> utentiRicerca;
private List<Ruolo> listaRuoli;

private Long idUtenteSelezionato;
private Utente utenteSelezionato;
private Long[] ruoliSelezionati;

@Action(value = "cerca",
results = {@Result(name = "input", location = "/WEB-INF/jsp/sicurezza/ricercaUtente.jsp"),
@Result(location = "/WEB-INF/jsp/sicurezza/ricercaUtente.jsp")})
public String cerca() {
setUtentiRicerca(utenteManager.ricercaUtente(getMatricola(), getCognome()));

return SUCCESS;
}

@Action(value = "associaRuoli",
results = {@Result(name = "input", type = "chain", params = {"method", "cerca"} ),
@Result(location = "/WEB-INF/jsp/sicurezza/ricercaUtente.jsp")})
public String associaRuoli() {
setUtenteSelezionato(utenteManager.findById(idUtenteSelezionato));

setListaRuoli(ruoloManager.findAll());

return ASSOCIARUOLI;
}

@Action(value = "inserisci",
results = {@Result(name = "input", type = "chain", params = {"method", "associaruoli"}),
@Result(location = "/WEB-INF/jsp/sicurezza/associaRuoli.jsp")})
public String inserisci() {




return SUCCESS ;
}

public void validateCerca() {
if (StringUtils.isBlank(getCognome()) && StringUtils.isBlank(getMatricola())) {
addActionError(getText("ricerca.campiObbligatori"));
}
}

public void validateAssociaRuoli() {
if (getIdUtenteSelezionato() == null) {
addActionError(getText("ricerca.radioObbligatorio"));

}

}

public void validateInserisci() {

if (getRuoliSelezionati() == null) {
addActionError(getText("ricerca.checkboxObbligatori"));

}

}


public String getCognome() {
return cognome;
}

public void setCognome(String cognome) {
this.cognome = cognome;
}


public String getMatricola() {
return matricola;
}

public void setMatricola(String matricola) {
this.matricola = matricola;
}


public List<Utente> getUtentiRicerca() {
return utentiRicerca;
}

public void setUtentiRicerca(List<Utente> utentiRicerca) {
this.utentiRicerca = utentiRicerca;
}

public Long getIdUtenteSelezionato() {
return idUtenteSelezionato;
}

public void setIdUtenteSelezionato(Long idUtenteSelezionato) {
this.idUtenteSelezionato = idUtenteSelezionato;
}

public Utente getUtenteSelezionato() {
return utenteSelezionato;
}

public void setUtenteSelezionato(Utente utenteSelezionato) {
this.utenteSelezionato = utenteSelezionato;
}

public List<Ruolo> getListaRuoli() {
return listaRuoli;
}

public void setListaRuoli(List<Ruolo> listaRuoli) {
this.listaRuoli = listaRuoli;
}

public Long[] getRuoliSelezionati() {
return ruoliSelezionati;
}

public void setRuoliSelezionati(Long[] ruoliSelezionati) {
this.ruoliSelezionati = ruoliSelezionati;
}
}


I have this action with validate method that i call when i do a submit and i have differente submit action in the js , now i want that :
after the jsp pass through the validate method forward on the same jsp with the same object selected?? how can it??
i try uses the type="chain" with parameter but don't function...
thanks
 
Richard Golebiowski
Ranch Hand
Posts: 213
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still not clear about what you want. Are you saying you want to return back to the same jsp page with the same radio button selected?
 
sol loc
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have this action that i call with the ricercaUtente.jsp:



package it.consip.cafe.sicurezza.webapp.action;

/**
* Created by IntelliJ IDEA.
* User: hp
* Date: 21-lug-2010
* Time: 16.44.50
* To change this template use File | Settings | File Templates.
*/

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.interceptor.annotations.After;
import it.consip.cafe.common.webapp.action.AbstractAction;
import it.consip.cafe.security.business.manager.RuoloManager;
import it.consip.cafe.security.business.manager.UtenteManager;
import it.consip.cafe.security.business.model.Ruolo;
import it.consip.cafe.security.business.model.Utente;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

import static org.apache.struts2.ServletActionContext.*;

@Results({
@Result(location = "/WEB-INF/jsp/sicurezza/ricercaUtente.jsp"),


@Result(name = "associaRuoli", location = "/WEB-INF/jsp/sicurezza/associaRuoli.jsp")
})

@Namespace("/sicurezza")
public class UtenteAction extends AbstractAction {


@Autowired
UtenteManager utenteManager;

@Autowired
RuoloManager ruoloManager;

private final String ASSOCIARUOLI = "associaRuoli";
private final String INSERISCI = "inserisci";
private String matricola;
private String cognome;

private List<Utente> utentiRicerca;
private List<Ruolo> listaRuoli;
private String setup;
private Long idUtenteSelezionato;
private Utente utenteSelezionato;
private Long[] ruoliSelezionati;

@Action(value = "cerca",
results = {@Result(name = "input", location = "/WEB-INF/jsp/sicurezza/ricercaUtente.jsp"),
@Result(location = "/WEB-INF/jsp/sicurezza/ricercaUtente.jsp")})
public String cerca() {
setUtentiRicerca(utenteManager.ricercaUtente(getMatricola(), getCognome()));

return SUCCESS;
}

@Action(value = "associaRuoli",
results = {@Result(name = "input", type = "chain", params = {"method", "cerca"} ),
@Result(location = "/WEB-INF/jsp/sicurezza/ricercaUtente.jsp")})
public String associaRuoli() {
setUtenteSelezionato(utenteManager.findById(idUtenteSelezionato));
getSetup();
setListaRuoli(ruoloManager.findAll());

return ASSOCIARUOLI;
}

@Action(value = "inserisci",
results = {@Result(name = "input", type = "chain", params = {"method", "associaruoli"}),
@Result(location = "/WEB-INF/jsp/sicurezza/associaRuoli.jsp")})
public String inserisci() {




return SUCCESS ;
}

public void validateCerca() {
if (StringUtils.isBlank(getCognome()) && StringUtils.isBlank(getMatricola())) {
addActionError(getText("ricerca.campiObbligatori"));
}
}

public void validateAssociaRuoli() {
if (getIdUtenteSelezionato() == null) {
addActionError(getText("ricerca.radioObbligatorio"));

}

}

public void validateInserisci() {

if (getRuoliSelezionati() == null) {
addActionError(getText("ricerca.checkboxObbligatori"));

}

}


public String getCognome() {
return cognome;
}

public void setCognome(String cognome) {
this.cognome = cognome;
}


public String getMatricola() {
return matricola;
}

public void setMatricola(String matricola) {
this.matricola = matricola;
}


public List<Utente> getUtentiRicerca() {
return utentiRicerca;
}

public void setUtentiRicerca(List<Utente> utentiRicerca) {
this.utentiRicerca = utentiRicerca;
}

public Long getIdUtenteSelezionato() {
return idUtenteSelezionato;
}

public void setIdUtenteSelezionato(Long idUtenteSelezionato) {
this.idUtenteSelezionato = idUtenteSelezionato;
}

public Utente getUtenteSelezionato() {
return utenteSelezionato;
}

public void setUtenteSelezionato(Utente utenteSelezionato) {
this.utenteSelezionato = utenteSelezionato;
}

public List<Ruolo> getListaRuoli() {
return listaRuoli;
}

public void setListaRuoli(List<Ruolo> listaRuoli) {
this.listaRuoli = listaRuoli;
}

public Long[] getRuoliSelezionati() {
return ruoliSelezionati;
}

public void setRuoliSelezionati(Long[] ruoliSelezionati) {
this.ruoliSelezionati = ruoliSelezionati;
}

public String getSetup() {
return setup;
}

public void setSetup(String setup) {
this.setup = setup;
}
}


La ricercaUtente.jsp:




<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://displaytag.sf.net" prefix="display" %>

<html>
<head>
<title>Ricerca Utente</title>
</head>
<body>

<meta name="immagine" content="<%= request.getContextPath() %>/img/logoSub4.jpg"/>
<s:form action="utente">
<div class="boxTable">


<s:actionerror/>
<fieldset class="form1">

<legend><strong>Cerca utente</strong></legend>

<s:label class="1200" for="matricola" key="ricerca.matricola"/>
<s:textfield name="matricola"/>
<br/>

<s:label class="1200" for="cognome" key="ricerca.cognome"/>
<s:textfield name="cognome"/>
<br/>

</fieldset>


<div class="tableButton3">
<s:submit action="cerca" key="default.pulsanteCerca"/>
</div>

</div>


<br/>
<s:if test="utentiRicerca.size > 0">
<display:table name="utentiRicerca" id="utente" class="dati testo11" summary="testo" style="width: 100%;">
<display:caption title="Tabella degli utenti"/>
<display:column property="matricola" title="Matricola" scope="col" style="text-align: center;"/>
<display:column property="cognome" title="Cognome"/>
<display:column property="nome" title="Nome"/>
<display:column property="stampaRuoli" title="Ruoli"/>
<display:column class="w1" style="text-align: center;" title="Seleziona">
<input type="radio" name="idUtenteSelezionato" value='<s:property value="#attr.utente.id"/>'/>
</display:column>
</display:table>

<div class="tableButton3">
<s:submit action="associaRuoli" key="default.pulsanteAssociaRuoli"/>

</div>

</s:if>
</s:form>
</body>
</html>




When i click a button submit associaRuoli and i go in the metho validateAssociaRuoli() and it fails i woult like to ridirect on the same page.


thankyou


solage
 
Richard Golebiowski
Ranch Hand
Posts: 213
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way validation works, if your set up is correct, when the validate method is called and you add the error then it should redirect back to the same page. If the error message is not set it will redirect to the next page. So there must be something wrong with your set up. Are you sure the validate method is being called?
 
sol loc
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


this is my action with the validate method :

package it.consip.cafe.sicurezza.webapp.action;

/**
* Created by IntelliJ IDEA.
* User: hp
* Date: 21-lug-2010
* Time: 16.44.50
* To change this template use File | Settings | File Templates.
*/

import it.consip.cafe.common.webapp.action.AbstractAction;
import it.consip.cafe.security.business.manager.RuoloManager;
import it.consip.cafe.security.business.manager.UtenteManager;
import it.consip.cafe.security.business.model.Ruolo;
import it.consip.cafe.security.business.model.Utente;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.*;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Results({
@Result(location = "/WEB-INF/jsp/sicurezza/ricercaUtente.jsp"),


@Result(name = "associaRuoli", location = "/WEB-INF/jsp/sicurezza/associaRuolo.jsp")
})

@Namespace("/sicurezza")
public class UtenteAction extends AbstractAction {


@Autowired
UtenteManager utenteManager;

@Autowired
RuoloManager ruoloManager;

private final String ASSOCIARUOLI = "associaRuoli";
private final String INSERISCI = "inserisci";
private String matricola;
private String cognome;

private List<Utente> utentiRicerca;
private List<Ruolo> listaRuoli;
private String setup;
private Long idUtenteSelezionato;
private Utente utenteSelezionato;
private Long[] ruoliSelezionati;

@Action(value = "cerca",
results = {@Result(name = "input", location = "/WEB-INF/jsp/sicurezza/ricercaUtente.jsp"),
@Result(location = "/WEB-INF/jsp/sicurezza/ricercaUtente.jsp")})
public String cerca() {
setUtentiRicerca(utenteManager.ricercaUtente(getMatricola(), getCognome()));

return SUCCESS;
}

@Action(value = "associaRuoli",
results = {@Result(name = "input", type = "chain", params = {"method", "cerca"} ),
@Result(location = "/WEB-INF/jsp/sicurezza/ricercaUtente.jsp")})
public String associaRuoli() {
setUtenteSelezionato(utenteManager.findById(idUtenteSelezionato));
getSetup();
setListaRuoli(ruoloManager.findAll());

return ASSOCIARUOLI;
}

@Action(value = "inserisci",
results = {@Result(name = "input", type = "chain", params = {"method", "associaruoli"}),
@Result(location = "/WEB-INF/jsp/sicurezza/associaRuolo.jsp")})
public String inserisci() {




return SUCCESS ;
}

//validate method
public void validateCerca() {
if (StringUtils.isBlank(getCognome()) && StringUtils.isBlank(getMatricola())) {
addActionError(getText("ricerca.campiObbligatori"));
}
}

public void validateAssociaRuoli() {
if (getIdUtenteSelezionato() == null) {
addActionError(getText("ricerca.radioObbligatorio"));

}

}

public void validateInserisci() {

if (getRuoliSelezionati() == null) {
addActionError(getText("ricerca.checkboxObbligatori"));

}

}


public String getCognome() {
return cognome;
}

public void setCognome(String cognome) {
this.cognome = cognome;
}


public String getMatricola() {
return matricola;
}

public void setMatricola(String matricola) {
this.matricola = matricola;
}


public List<Utente> getUtentiRicerca() {
return utentiRicerca;
}

public void setUtentiRicerca(List<Utente> utentiRicerca) {
this.utentiRicerca = utentiRicerca;
}

public Long getIdUtenteSelezionato() {
return idUtenteSelezionato;
}

public void setIdUtenteSelezionato(Long idUtenteSelezionato) {
this.idUtenteSelezionato = idUtenteSelezionato;
}

public Utente getUtenteSelezionato() {
return utenteSelezionato;
}

public void setUtenteSelezionato(Utente utenteSelezionato) {
this.utenteSelezionato = utenteSelezionato;
}

public List<Ruolo> getListaRuoli() {
return listaRuoli;
}

public void setListaRuoli(List<Ruolo> listaRuoli) {
this.listaRuoli = listaRuoli;
}

public Long[] getRuoliSelezionati() {
return ruoliSelezionati;
}

public void setRuoliSelezionati(Long[] ruoliSelezionati) {
this.ruoliSelezionati = ruoliSelezionati;
}

public String getSetup() {
return setup;
}

public void setSetup(String setup) {
this.setup = setup;
}
}

what i was wrong??

thank you
 
Richard Golebiowski
Ranch Hand
Posts: 213
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add the following method to your action class and let me know what happens.

 
sol loc
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use
# public void validate(){
# addActionError("Test error!!");
# }
it work only with a first :<div class="tableButton3">
<s:submit action="cerca" key="default.pulsanteCerca"/>
</div>

but i wold like to handle the validatio with different method and then to handle different riderect

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://displaytag.sf.net" prefix="display" %>

<html>
<head>
<title>Ricerca Utente</title>
</head>
<body>

<meta name="immagine" content="<%= request.getContextPath() %>/img/logoSub4.jpg"/>
<s:form action="utente">
<div class="boxTable">


<s:actionerror/>
<fieldset class="form1">

<legend><strong>Cerca utente</strong></legend>

<s:label class="1200" for="matricola" key="ricerca.matricola"/>
<s:textfield name="matricola"/>
<br/>

<s:label class="1200" for="cognome" key="ricerca.cognome"/>
<s:textfield name="cognome"/>
<br/>

</fieldset>


<div class="tableButton3">
<s:submit action="cerca" key="default.pulsanteCerca"/>
</div>

</div>


<br/>
<s:if test="utentiRicerca.size > 0">
<display:table name="utentiRicerca" id="utente" class="dati testo11" summary="testo" style="width: 100%;">
<display:caption title="Tabella degli utenti"/>
<display:column property="matricola" title="Matricola" scope="col" style="text-align: center;"/>
<display:column property="cognome" title="Cognome"/>
<display:column property="nome" title="Nome"/>
<display:column property="stampaRuoli" title="Ruoli"/>
<display:column class="w1" style="text-align: center;" title="Seleziona">
<input type="radio" name="idUtenteSelezionato" value='<s:property value="#attr.utente.id"/>'/>
</display:column>
</display:table>

<div class="tableButton3">
<s:submit action="associaRuoli" key="default.pulsanteAssociaRuoli"/>

</div>

</s:if>
</s:form>
</body>
</html>




 
Richard Golebiowski
Ranch Hand
Posts: 213
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but i wold like to handle the validatio with different method and then to handle different riderect


Struts only knows to use the validate() method, it dose not know about your other methods. You need a different approach to what you are trying to accomplish.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic