使用Spring MVC在JSP中显示列表

时间:2017-04-04 18:18:43

标签: java spring jsp spring-mvc

我试图通过主键(catCodes)显示数据库表中的所有行,但是我收到NullPointerException错误。我可以通过指定主键显示一行,但无法显示所有行。

JSP:

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Widget.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Output.H>
#include <string>
using namespace std;
Fl_Output* out;

int sum=0;
char ssum;

void cb_send(Fl_Widget* w, void *v)
{
    if (((Fl_Button*)w)->value() == 0)
    {
        w->hide();
        Fl_Input* inp = (Fl_Input*)v;
        sum = sum + atoi(inp->value());
        ssum = sum;
        out->value(ssum);
        //out->value(inp->value()); this works 
        //out->value( out->value() + inp->value() ); this should theoretically work?
    }
}
int main(int argc, char **argv)
{
    Fl_Window *winmain = new Fl_Window(500, 500);
    Fl_Button *but1 = new Fl_Button(100, 100, 50, 50, "1");
    Fl_Button *but2 = new Fl_Button(100, 200, 50, 50, "2");

    Fl_Input *no1 = new Fl_Input(170, 100, 150, 50, "");
    Fl_Input *no2 = new Fl_Input(170, 200, 150, 50, "");
    out = new Fl_Output(170, 300, 150, 50, "");
    but1->callback((Fl_Callback*) cb_send, no1);
    but2->callback((Fl_Callback*) cb_send, no2);
    winmain->resizable();
    winmain->end();
    winmain->show(argc, argv);

    return Fl::run();
}

控制器:

<table class="table table-hover table-striped-rows table-bordered table-condensed">
                    <thead>
                        <tr>
                            <td><b>Category Description</b></td>
                            <td><b>Category Audit Date</b></td>
                            <td><b>Category Audit Location</b></td>
                        </tr>
                    </thead>
                    <c:forEach var="listValue" items="${eligCatsForm.eligCats}">
                    <tr>
                        <td>
                            <c:out value="${listValue.catDesc}" /> 
                        </td>
                        <td>
                            <c:out value="${listValue.catAuditDate}" /> 
                        </td>
                        <td>
                            <c:out value="${listValue.catAuditLocation}" /> 
                        </td>
                    </tr>
                    </c:forEach>
                </table>

服务:

@ActionID(action = ActionTypes.NO_LOG)
@RequestMapping(value ="displayEligCats",method = RequestMethod.GET)
@PreAuthorize("hasAnyRole('ROLE_EMS_NOTE_VIEWER','ROLE_EMS_NOTE_USER','ROLE_EMS_NOTE_UPDATE_USER')")
public String displayEligCats(@RequestParam(value="catCode", required=false) Long catCode, Model model) {

    try {

    List<EligibilityCategories> eligCats = eligibilityCategoriesService.getEligCats(catCode);
    EligibilityCategoriesForm eligCatsForm = new EligibilityCategoriesForm ();
    eligCatsForm.setEligCats(eligCats);

    model.addAttribute("eligCatsForm", eligCatsForm);

    logger.debug("Exiting AdminReviewController.displayEligNotes");
    return WebConstants.RETURN_ELIG_CATS;

    } catch (Exception e) {
        if(logger.isErrorEnabled())
        {
            logger.error(e);
        }
        throw new ErrorException(e.getMessage());
    }   
}

}

ServiceImpl:

public interface EligibilityCategoriesService {
List<EligibilityCategories> getEligCats(long catCode);

}

DAO:

@Service("eligibilityCategories")
public class EligibilityCategoriesServiceImpl implements                     EligibilityCategoriesService {

protected final Log logger = LogFactory.getLog(getClass());

@Autowired
EligibilityCategoriesDao eligCatsDao;

@Autowired
private CoreDao coreDao;

@Override
public List<EligibilityCategories> getEligCats(long catCode) {
    return eligCatsDao.getEligCats(catCode);

}

}

JPA DAO:

public interface EligibilityCategoriesDao {
final static String FIND_ELIG_CATS_BY_CAT_CODE = "EligibilityCategories.findEligCatsByCatCode";

List<EligibilityCategories> getEligCats(long catCode);

}

形式:

@Repository
public class JPAEligibilityCategoriesDao implements EligibilityCategoriesDao {

protected final Log logger = LogFactory.getLog(getClass());

@PersistenceContext(unitName = "emf")
private EntityManager entityManager;

@Override
public List<EligibilityCategories> getEligCats(long catCode) {
    Query query = entityManager.createNamedQuery(FIND_ELIG_CATS_BY_CAT_CODE);
    query.setParameter(QueryParameterConstants.QPC_CAT_CODE, catCode);
    @SuppressWarnings("unchecked")
    List<EligibilityCategories> eligCats = (List<EligibilityCategories>) query.getResultList();
    return eligCats;
}

}

域:

public class EligibilityCategoriesForm {

private Long catCode;
private List<EligibilityCategories> eligCats;

public Long getCatCode() {
    return catCode;
}

public void setCatCode(Long catCode) {
    this.catCode = catCode;
}

public List<EligibilityCategories> getEligCats() {
    return eligCats;
}

public void setEligCats(List<EligibilityCategories> eligCats) {
    this.eligCats = eligCats;
}

}

任何建议都会非常有帮助。我是java和Spring的新手,所以请不要笑。

1 个答案:

答案 0 :(得分:0)

您将参数catCode声明为基本类型long:

getEligCats(long catCode);

但在Controller类中,参数catCode被声明为Long个对象。

当您致电displayEligCats以获取&#34;所有类别&#34;如果没有指定任何catCode,它的参数将是null,并且它不能被&#34; casted&#34;原始类型很长。

在服务和dao中更改方法定义的签名,如:

getEligCats(Long catCode);

请注意检查catCode参数的null值。

我希望它可以帮助你,再见。