이것은 Spring MVC Mongodb 프로젝트의 Model 클래스입니다.
public class SampleProject{
@Id
String id;
public String getId() {
return id;
}
String regNo;
String name;
ArrayList<String> result;
public String getRegNo() {
return regNo;
}
public String getName() {
return name;
}
public ArrayList<String> getResult() {
return result;
}
}
이것이 내 서비스 클래스의 모습입니다.
@Repository("resultService")
public class ResultService {
@Autowired
private MongoTemplate mongoTemplate;
public static final String COLLECTION_NAME = "collectionName";
public StudentResult getStudentResult(String regNo) {
Query query = new Query(Criteria.where("regNo").is(regNo));
StudentResult Ek = mongoTemplate.findOne(query, StudentResult.class);
return Ek;
}
}
이것이 내 컨트롤러 클래스의 모습입니다.
public class ResultController {
private final ResultService resultService;
@Autowired
public ResultController(ResultService resultService) {
this.resultService = resultService;
}
@RequestMapping(value ="/search", method = RequestMethod.GET)
public String getStudentResult(String regNo, ModelMap model){
model.addAttribute("studentResult",resultService.getStudentResult(regNo));
return "output";
}
}
이것이 내 JSP의 모습입니다.
<%@ taglib prefix="c" uri="Oracle Technology Network for Java Developers" %>
<c:out value="${studentResult.name}"/>
<c:out value="${studentResult.regNo}"/>
<c:out value="${studentResult.deptName}"/>
<c:out value="${studentResult.collName}"/>
<c:out value="${studentResult.result}"/> <!--This line prints the arraylist as arraylist.-->
<!--But the following iteration using foreach doesn't work -->
<c:forEach var="student" items="${studentResult}">
<tr>
<td>${student.name}</td>
<td>${student.regNo}</td>
<td><input type="button" value="delete" onclick="window.location='person/delete?id=${person.id}'"/></td>
</tr>
</c:forEach>
이것은 db가 보이는 것입니다
{ "_id": ObjectId ( "53f017f2300469e091855f0d"), "regNo": "3103", "name": "ABCD", "collName": "INSTITUTE OF ENGINEERING AND TECHNOLOGY", "deptName": "B.Tech Technology" , "result": [{ "subjCode": "CY6251", "grade": "E", "result": "PASS", "subjName": "Engineering Chemistry – II"}, { "subjCode": "GE6151 ","grade ":"E ","result ":"PASS ","subjName ":"컴퓨터 프로그래밍 "}, {"subjCode ":"GE6252 ","grade ":"D ","result ":" PASS ","subjName ":"기초 전기 및 전자 공학 "},{ "subjCode": "GE6253", "grade": "E", "result": "PASS", "subjName": "Engineering Mechanics"},]}
이제 JSP에서 arraylist를 반복하여 arraylist에서 각 주제 이름과 등급을 표시 할 수 있습니다.
내가 말한대로 내 taglib 선언을 변경했습니다. 그러나 그가 말한대로 변경하기 전과 변경 한 후에 내 jsp에 "Unknown tag (c : forEach)"가 표시됩니다. Stackoverflow의 대부분의 답변에 따라 해결할 수 없었습니다.
문제는 내 결과 ArrayList가 String 유형이 아니라는 것입니다. 그것은 내가 subjCode, subjName을 String으로 가지고있는 결과 유형입니다. 이것은 문제를 해결했습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다