データベーステーブルからすべての行をプライマリキー(catCodes)で表示しようとしていますが、NullPointerExceptionエラーが発生します。プライマリキーを指定して、すべての行を表示できない場合は、1行を表示できます。Spring MVCを使用してJSPでリストを表示
JSP:
<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());
}
}
サービス:
public interface EligibilityCategoriesService {
List<EligibilityCategories> getEligCats(long catCode);
}
ServiceImpl:
@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);
}
}
DAO:
public interface EligibilityCategoriesDao {
final static String FIND_ELIG_CATS_BY_CAT_CODE = "EligibilityCategories.findEligCatsByCatCode";
List<EligibilityCategories> getEligCats(long catCode);
}
JPA DAO:
@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;
}
}
ドメイン:
@Entity
@Table(name="TSEM_ELIG_CATS")
public class EligibilityCategories implements java.io.Serializable {
private long catCode;
private String catDesc;
private Integer catVerNum;
private Long catAuditUser;
private Date catAuditDate;
private String catAuditAction;
private String catAuditLocation;
public EligibilityCategories() {
}
public EligibilityCategories(long catCode, String catDesc, Integer catVerNum,
Long catAuditUser, Date catAuditDate, String catAuditAction,
String catAuditLocation) {
this.catCode = catCode;
this.catDesc = catDesc;
this.catVerNum = catVerNum;
this.catAuditUser = catAuditUser;
this.catAuditDate = catAuditDate;
this.catAuditAction = catAuditAction;
this.catAuditLocation = catAuditLocation;
}
@Id
@Column(name = "CAT_CODE", unique = true, nullable = false, precision = 10, scale = 0)
public long getCatCode() {
return this.catCode;
}
public void setCatCode(long catCode) {
this.catCode = catCode;
}
@Column(name = "CAT_DESC", nullable = false, length = 2000)
public String getCatDesc() {
return this.catDesc;
}
public void setCatDesc(String catDesc) {
this.catDesc = catDesc;
}
@Column(name = "CAT_VER_NUM", precision = 5, scale = 0)
public Integer getCatVerNum() {
return this.catVerNum;
}
public void setCatVerNum(Integer catVerNum) {
this.catVerNum = catVerNum;
}
@Column(name = "CAT_AUDIT_USER", precision = 10, scale = 0)
public Long getCatAuditUser() {
return this.catAuditUser;
}
public void setCatAuditUser(Long catAuditUser) {
this.catAuditUser = catAuditUser;
}
@Temporal(TemporalType.DATE)
@Column(name = "CAT_AUDIT_DATE", length = 7)
public Date getCatAuditDate() {
return this.catAuditDate;
}
public void setCatAuditDate(Date catAuditDate) {
this.catAuditDate = catAuditDate;
}
@Column(name = "CAT_AUDIT_ACTION", length = 1)
public String getCatAuditAction() {
return this.catAuditAction;
}
public void setCatAuditAction(String catAuditAction) {
this.catAuditAction = catAuditAction;
}
@Column(name = "CAT_AUDIT_LOCATION", length = 20)
public String getCatAuditLocation() {
return this.catAuditLocation;
}
public void setCatAuditLocation(String catAuditLocation) {
this.catAuditLocation = catAuditLocation;
}
}
何かアドバイスはとても参考になります。私はJavaとSpringの初心者ですので、笑わないでください。
質問を編集し、例外スタックトレースを含めてください。 –