java.lang.ClassCastException:org.apache.struts.validator.DynaValidatorForm 无法转换

问题描述 投票:0回答:1

我想从数据库中的JSP表单中搜索数据并将其显示在另一个JSP文件中...

javax.servlet.ServletException: java.lang.ClassCastException: org.apache.struts.validator.DynaValidatorForm cannot be cast to org.mat.search.SearchFB
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:286)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

SearchFB.java

import org.apache.struts.validator.DynaValidatorForm;

public class SearchFB extends DynaValidatorForm {

private String userName;
private String caste[];
private String qualification[];
private Integer age;
public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}
    .
    .

SearchAction.java

public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) {

    SearchFB sfb = (SearchFB) form;
    Formdata fd = new Formdata();
    fd.setUserName((String) sfb.get("userName"));
    fd.setAge((Integer) sfb.get("age"));
    fd.setQualification((String)sfb.get("qualification"));
    fd.setCaste((String) sfb.get("caste"));

    SearchDTO sdto = new SearchDTO();
    SearchJB sjb = new SearchJB();
    sjb.searchProduct(sdto);
    return mapping.findForward("ssuccess");
}
}

Formdata.java

public Formdata extends ActionForm{        
private String userName;
private String qualification;
private Integer age;
private String caste;


public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}
    setters and getters...
java jsp struts struts-1 actionform
1个回答
0
投票

如果您使用

DynaValidatorForm
那么它应该被转换为 Struts 提供的动作形式。例如

DynaValidatorForm sfb = (DynaValidatorForm) form;

它将解决您的类转换异常,但您作为参数传递给其他层的实例未初始化,因此该操作本身是可疑的。

© www.soinside.com 2019 - 2024. All rights reserved.