0

我正在使用 struts 2 框架开发一个 web 应用程序,并且我在 jsp 中遇到了 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中有那个select标签,当按下提交时,动作不会从选择中获取任何数据。

我希望有人可以帮助我,因为它让我发疯。如果您需要更多信息,请告诉我,我会尽快发布。

提前致谢。

编辑:

这是 Cita.class 代码的一部分。它还具有每个属性的设置器和获取器。

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:

这是我的登录拦截器:

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";
    }
}
}

如果您需要其他东西,请告诉我:-)

4

2 回答 2

0

看起来像一个拦截器问题。

是什么myStack

这是我在项目中使用的示例堆栈(使用登录拦截器)

<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>

loginStack 被用作该包中所有操作的 defaultStack,因为<default-interceptor-ref name="loginStack"/>

于 2013-08-24T18:44:31.967 回答
0

好的,伙计们,看来我已经解决了。感谢coding_idiot的回答,我能够找出问题所在。似乎我自己的堆栈中缺少 defaultStack 是问题所在。我刚刚添加了 defaultStack,现在看起来工作正常。所以,非常感谢大家,特别是coding_idiot。代码最终看起来像:

<interceptors>
        <interceptor name="logger" class="interceptor.LoginInterceptor"/>
        <interceptor-stack name="myStack">
            <interceptor-ref name="logger"/>
            <interceptor-ref name="defaultStack"/>
        </interceptor-stack>
</interceptors>
于 2013-08-25T12:29:36.980 回答