我正在使用struts 2框架开发一个Web应用程序,我在jsp中面临选择标记的问题。 select标签似乎工作正常,但只要按下提交按钮,我的操作就无法从选择中获取数据。 我已经查找了有关select的问题,但所有这些似乎都与列表有关。 在我的情况下,列表显示正常。所以,我很失落,我不知道该怎么做。
这是我的jsp代码和我的动作代码。当我按下提交时,它假设表单将被发送到我的操作,并且我应该从表单中获取数据,但是当我打印数据时总是为空。
JSP代码:
<s:form action="borrarCita" method="post">
<s:select id="idCita" name="idCita" list="pendings" listKey="idCita" listValue="fecha" label="Elija una cita a anular"></s:select>
<s:submit value="Aceptar"/>
</s:form>
行动代码:
public class CitasAction extends ActionSupport implements SessionAware{
private static final long serialVersionUID = 1L;
private Map session = ActionContext.getContext().getSession();
private List pendings= new ArrayList<Cita>();
private String idCita;
private String desplegarCitasPendientes;
private String pedirCita;
private String consultarCitas;
private String citaAnulada;
public String execute(){
return SUCCESS;
}
public String pedirCita(){
setCitaAnulada("");
setDesplegarCitasPendientes("");
setConsultarCitas("");
DbService db = (DbService)session.get("db");
Paciente paciente = (Paciente)session.get("Paciente");
List listaMedicos = db.getListaMedicos();
session.remove("listaCitas");
session.put("listaMedicos", listaMedicos);
setPedirCita("ok");
return SUCCESS;
}
public String desplegarCitasPendientes(){
setCitaAnulada("");
setPedirCita("");
setConsultarCitas("");
Paciente pac = (Paciente)session.get("user");
Iterator<Cita> nombreIterator = pac.getCitas().iterator();
while(nombreIterator.hasNext()){
Cita primera = nombreIterator.next();
if(primera.getStatus().equals("Pending")){
getPendings().add(primera);
}
}
setDesplegarCitasPendientes("ok");
return SUCCESS;
}
public String borrarCita(){
setConsultarCitas("");
setPedirCita("");
setDesplegarCitasPendientes("");
System.out.println(getIdCita());
System.out.println(ActionContext.getContext().getParameters().toString());
Paciente pac = (Paciente)session.get("user");
/*
Iterator<Cita> iterator = pac.getCitas().iterator();
while(iterator.hasNext()){
Cita primera = iterator.next();
if(primera.getFecha().equals(getFecha())){
iterator.remove();
break;
}
}
pac.setCitas(getPendings());
pac.getCitas().remove(getCitaSeleccionada());
session.put("user", pac);
DbService db = (DbService)session.get("db");
db.uploadPaciente(pac);*/
setCitaAnulada("ok");
return SUCCESS;
}
public String consultarCitas(){
setPedirCita("");
setDesplegarCitasPendientes("");
setCitaAnulada("");
setConsultarCitas("ok");
return SUCCESS;
}
public Map getSession() {
return session;
}
public void setSession(Map session) {
this.session = session;
}
public List getPendings() {
return pendings;
}
public void setPendings(List pendings) {
this.pendings = pendings;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public String getDesplegarCitasPendientes() {
return desplegarCitasPendientes;
}
public void setDesplegarCitasPendientes(String desplegarCitasPendientes) {
this.desplegarCitasPendientes = desplegarCitasPendientes;
}
public String getPedirCita() {
return pedirCita;
}
public void setPedirCita(String pedirCita) {
this.pedirCita = pedirCita;
}
public String getConsultarCitas() {
return consultarCitas;
}
public void setConsultarCitas(String consultarCitas) {
this.consultarCitas = consultarCitas;
}
public String getCitaAnulada() {
return citaAnulada;
}
public void setCitaAnulada(String citaAnulada) {
this.citaAnulada = citaAnulada;
}
public String getIdCita() {
return idCita;
}
public void setIdCita(String idCita) {
this.idCita = idCita;
}
}
因此,总而言之,我在jsp中有选择标记,当按下提交时,操作不会从选择中获取任何数据。
我希望有人可以帮助我,因为它让我发疯。 如果您需要更多信息,请告诉我,我会尽快发布。
提前致谢。
编辑:
这是Cita.class代码的一部分。它还具有每个属性的setter和getter。
public class Cita implements Serializable{
@Id @GeneratedValue
@Column(name="CITA_ID")
private int idCita;
@Column(name="FECHA")
@Temporal(TemporalType.DATE)
private Date fecha;
@Column(name="HORA")
@Temporal(TemporalType.TIME)
private Date hora;
@Column(name="ESTADO")
private String status;
@Column(name="ESPECIALIDAD")
private String especialidad;
@ManyToOne
@JoinColumn(name="MEDICO_ID")
private Medico medico;
@ManyToOne
@JoinColumn(name="PACIENTE_ID")
private Paciente paciente;
这是struts.xml:
<interceptors>
<interceptor name="logger" class="interceptor.LoginInterceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="logger"/>
</interceptor-stack>
</interceptors>
<action name="desplegarCitasPendientes" class="action.CitasAction" method="desplegarCitasPendientes">
<interceptor-ref name="myStack"/>
<result name="success">/citas.jsp</result>
<result name="login" type="redirect">/index.jsp</result>
<result name="input">/index.jsp</result>
</action>
<action name="borrarCita" class="action.CitasAction" method="borrarCita">
<interceptor-ref name="myStack"/>
<result name="success">/citas.jsp</result>
<result name="login" type="redirect">/index.jsp</result>
<result name="input">/index.jsp</result>
</action>
@Jeroen,对于巨大的代码墙感到抱歉。这是我在这里的第一篇文章。
@Roman,我希望这有助于你帮助我: - )
感谢您的回复。
编辑2:
这是我的LoginInterceptor:
public class LoginInterceptor extends AbstractInterceptor {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String intercept(final ActionInvocation invocation) throws Exception {
Map<String, Object> session = invocation.getInvocationContext().getSession();
// sb: if the user is already signed-in, then let the request through.
if (session.get("user") != null) {
return invocation.invoke();
}else{
System.out.println("redirect");
return "login";
}
}
}
如果您需要别的东西,请告诉我: - )
答案 0 :(得分:0)
看起来像拦截器问题。
什么是myStack
?
这是我在项目中使用的示例堆栈(使用Login Interceptor)
<package name="test" extends="struts-default">
<interceptors>
<interceptor name="nlogin" class="interceptors.LoginInterceptor"/>
<interceptor-stack name="loginStack">
<interceptor-ref name="nlogin"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="loginStack"/>
<action name="test-list" class="actions.InboxAction">
<result>/pages/inbox/list.jsp</result>
</action>
</package>
由于<default-interceptor-ref name="loginStack"/>
,loginStack被用作该包中所有操作的defaultStack
答案 1 :(得分:0)
<interceptors>
<interceptor name="logger" class="interceptor.LoginInterceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="logger"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>