The server encountered an unexpected condition that prevented it from fulfilling the request - spring

I am trying to display Data from DB.But Shows error as
The server encountered an unexpected condition that prevented it from
fulfilling the request
Exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/pages/Detail.jsp at line 14
11: </head>
12: <body>
13:
14: <c:forEach var="x" items="${prdt}">
15: <table>
16: <img src="resources/Images/${x.id}.png"/>
17: <td>"
MY JSP
<c:forEach var="x" items="${prdt}">
<table>
<img src="resources/Images/${x.id}.png"/>
<td>
<c:out value="${x.product_Name}"/></td>
<td>
<c:out value="${x.descripction}"/></td>
<td>
<c:out value="${x.price}"/></td>
<td>
<c:out value="${x.mfg_Date}"/>
</td>
</table>
</c:forEach>
My Controller
public ModelAndView productDtails(#PathVariable int id)
{
ModelAndView model=new ModelAndView("Detail");
model.addObject("prdt",pd.getById(id));
return model;
}
My DAO IMpl
public Product getById(int id)
{
Session session=sessionFactory.openSession();
Product p=(Product) session.get(Product.class, id);
session.close();
return p;
}
Any Idea????

You can't iterate over prdt object i.e., you are using forEach tag and prdt is not a List object, so to solve the issue simply remove <c:forEach var="x" items="${prdt}"> or else you need to return a list object from your Contoller.
Your JSP looks as below (after removing <c:forEach):
<table>
<img src="resources/Images/${x.id}.png"/>
<td>
<c:out value="${prdt.product_Name}"/></td>
<td>
<c:out value="${prdt.descripction}"/></td>
<td>
<c:out value="${prdt.price}"/></td>
<td>
<c:out value="${prdt.mfg_Date}"/>
</td>
</table>

My issue was resolved by adding the below in the model
[AllowHtml]
public string Description { get; set; }
C# MVC for POST

Related

How to show set of Arrays in one <TD> in Spring Boot / JSP?

I would like to show all the right answers(Green) in one TD if there are more than one and all the wrong answers(Red) in one TD if there are more than one. Is there anyway in the controller or at the front end that I can achieve this?
With my current code this is what I get.
The aim is to display like this
Controller
#GetMapping("/quesInAss/{id}")
public String quesInAssessment(#PathVariable("id") Integer id, Model model, HttpSession session) {
Integer insId = (Integer) session.getAttribute("instId");
List<Answers> rlist = new ArrayList<Answers>();
List<Answers> wlist = new ArrayList<Answers>();
Assessments ass = as.getAllByAssInst(id, insId);
model.addAttribute("assName", ass.getAssName());
List<Questions> queslist = qs.getQuesByAssessment(ass);
for (Questions ques : queslist) {
List<Answers> anslist = ansService.getAnswersByQuestion(ques);
for (Answers answer : anslist) {
if (answer.getAnsStatusCode().equals("CORR")) {
rlist.add(answer);
Answers[] array = new Answers[rlist.size()];
array = rlist.toArray(array);
model.addAttribute("rightAnsList", array);
} else {
wlist.add(answer);
Answers[] array = new Answers[wlist.size()];
array = wlist.toArray(array);
model.addAttribute("wrongAnsList", array);
}
}
}
model.addAttribute("queslist", queslist);
return "listOfQuesInAss";
}
JSP
<c:forEach items="${queslist}" var="ques">
<tr>
<td>${ques.quesText}</td>
<c:forEach items="${rightAnsList}" var="rightans">
<c:if test="${rightans.questions.quesId == ques.quesId}">
<td>${rightans.answer}</td>
</c:if>
</c:forEach>
<c:forEach items="${wrongAnsList}" var="wrongans">
<c:if test="${wrongans.questions.quesId == ques.quesId}">
<td>${wrongans.answer}</td>
</c:if>
</c:forEach>
</tr>
</c:forEach>
I'm not sure if I could understand your question right, try this:
<c:forEach items="${queslist}" var="ques">
<tr>
<td>${ques.quesText}</td>
<td>
<c:forEach items="${rightAnsList}" var="rightans" varStatus="status">
<c:if test="${rightans.questions.quesId == ques.quesId}">
${rightans.answer}
</c:if>
<c:if test="${not status.last}">,</c:if>
</c:forEach>
</td>
<td>
<c:forEach items="${wrongAnsList}" var="wrongans" varStatus="status">
<c:if test="${wrongans.questions.quesId == ques.quesId}">
${wrongans.answer}
</c:if>
<c:if test="${not status.last}">,</c:if>
</c:forEach>
</td>
</tr>
</c:forEach>

Displaying list in a list JSP Page

I am trying to display a list and every object of that list has another list which I have to display, the values are coming but are not being displayed, below is the code I have tried if someone could identify what I am doing wrong, it will be very helpful. I am using Spring MVC but I don't think it has anything to do with this error, any help will be appreciated.
#Entity
#Table(name="FILE_TRACKING_MANAGEMENT")
#NamedQuery(name="FileTrackingManagement.findAll", query="SELECT f FROM FileTrackingManagement f")
{
#OneToMany(mappedBy="fileTrackingManagement")
private List<FileNoting> fileNotings;
//bi-directional many-to-one association to FileTrackingFile
#OneToMany(mappedBy="fileTrackingManagement")
private List<FileTrackingFile> fileTrackingFiles;
}
My Controller Class:
#RequestMapping("viewMarkedFiles/list")
public ModelAndView viewMarkedFiles(Model model)
{
List<FileTrackingManagement> markedFileList= fileService.getList();
//model.addAttribute("markedFileList", markedFileList);
return new ModelAndView("viewMarkedFiles", "markedFileList", markedFileList);
}
My JSP Page:
<table border="1" bgcolor="black" width="600px">
<c:forEach items="${markedFileList}" var="markedFileVal">
<%-- <c:set var="fileNotings" value="${jobs[i.index].jobId}"/> --%>
<tr
style="background-color: white; color: black; text-align: center;"
height="30px">
<td><c:out value="${markedFileVal.diarynumber}" />
<td><c:out value="${markedFileVal.subject}" />
<td>
</td>
<td>
</td>
<td>
<c:forEach items="${markedFileList.fileNotings}" var="filenotings">
<c:out value="${filenotings.notingData}" /></c:forEach>
</td>
</c:forEach>
</table>
It throws this exception:
45: <%-- <c:out value="${fileVal.description}" /> --%>
46: </td>
47: <td>
48: <c:forEach items="${markedFileList.fileNotings}" var="filenotings">
49: <c:out value="${filenotings.notingData}" /></c:forEach>
50:
51:
Stacktrace:] with root cause
java.lang.NumberFormatException: For input string: "fileNotings"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
Thanks in advance
<c:forEach items="${markedFileList}" var="markedFileVal">
So, you're iterating oover the list markedFileList. Inside the body of the tag, the current element is markedFileVal.
Then you do
<c:forEach items="${markedFileList.fileNotings}" var="filenotings">
So you're ietarting over markedFileList.fileNotings. But markedFileList is a list. It's not the current element. What you want is
<c:forEach items="${markedFileVal.fileNotings}" var="filenoting">
Also note that I removed the final s from filenotings. This variable points to a single filenoting. Not to several ones.
#OneToMany(fetch = FetchType.EAGER, mappedBy="fileTrackingManagement")
private List fileNotings;
The value of fetchType is Lazy by default, when I changed its value, it worked

ModelAttribute not working with lists in spring

I want to bind a List using ModelAttribute. The list contains objects of type Transaction each of which contains transactionid (type int). This is my controller code:
#RequestMapping(value = "/approvecreditdebit.do", method = RequestMethod.POST)
public ModelAndView doActions(HttpServletRequest request,
#ModelAttribute("clc") Clc transactionList, BindingResult result,
ModelMap model) {
/*
* switch (action) { case "approve":
*/
System.out.println("Obj = " + transactionList.getClass());
System.out.println("Val = " + transactionList.getTransactionList());
Users users = new Users();
return new ModelAndView("internal","internal",users);
}
This is my jsp code:
<form:form action="approvecreditdebit.do" method="POST"
modelAttribute="clc">
<table border="1">
<tr>
<th>no</th>
<th>ID</th>
</tr>
<c:forEach items="${clc.transactionList}"
var="transaction" varStatus="status">
<tr>
<td>${status.index}</td>
<td><input name = "transaction[${status.index}].transactionId"
value="${transaction.transactionId}" /></td>
</tr>
</c:forEach>
</table>
<br>
<br>
<center>
<input type="submit" value="approve" />
</center>
</form:form>
This is the Clc class:
public class Clc{
private List<Transaction> transactionList;
public List<Transaction> getTransactionList() {
return transactionList;
}
public void setTransactionList(List<Transaction> transactionList) {
this.transactionList = transactionList;
}
}
The value of transactionList is not being set to the values received from the form. I receive the following error:
Request processing failed; nested exception is java.lang.NullPointerException
I tried searching for the solution on google and got a lot of solutions from stackoverflow but none of them seem to work.
Try something like this (notice the use of <form:input>). I just tried it on a simple Spring MVC app and it works (list is not null and has the values from the form when I try to access it in my POST method).
<c:forEach var="transaction" varStatus="status" items="${clc.transactionList}">
<tr>
<td>${status.index}</td>
<td><form:input path="transactionList[${status.index}].id" /></td>
</tr>
</c:forEach>

Spring form validation - errors are not displayed

I have following situation:
I try to create a simple form to edit information about a movie. Therefor I use spring mvc with jsp.
For the validation I use the JSR 303 hibernate implementation (hibernate-validator 4.2.0).
My problem is that if there are validation errors (BindResults hasErrors method returns true) I can only display the errors with
<form:errors path="*" />
and not fieldspecific. like with:
<form:errors path="title" />
I have no idea why but all errors are displayed in at the path="*" errortag, but none at the fieldspecific ones. The only errors which are displayed right to the field are some which brought an exception i.e.
Failed to convert property value of type java.lang.String to required type int for property runtime; nested exception is java.lang.NumberFormatException: For input string: "")
the important parts of my jsp file look like this:
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<form:form commandName="movie">
<fieldset>
<form:errors path="*" cssClass="formErrorBlock"/>
<table>
<tr>
<td><form:label path="title">Title:</form:label></td>
<td>
<form:input path="title"/>
<form:errors path="title" cssClass="formFieldError"/>
</td>
</tr>
<tr>
<td><form:label path="runtime">Runtime:</form:label></td>
<td>
<form:input path="runtime"/><br />
<form:errors path="runtime" cssClass="formFieldError" />
</td>
</tr>
<tr>
<td><form:label path="imdbId">IMDB-ID:</form:label></td>
<td>
<form:input path="imdbId"/><br />
<form:errors path="imdbId" cssClass="formFieldError" />
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" />
</td>
</tr>
</table>
</fieldset>
</form:form>
My Controller:
#Controller
public class MovieController {
#Autowired
private MovieService _movieService;
//some other methods...
#RequestMapping(value="/movie/{pMovieId}/edit", method = RequestMethod.GET)
public String showForm(ModelMap pModel, #PathVariable Integer pMovieId) {
Movie movie = _movieService.getMovieById(pMovieId);
pModel.addAttribute("movie", movie);
return "edit/movie";
}
#RequestMapping(value="/movie/{pMovieId}/edit", method = RequestMethod.POST)
public String editMovieSave(#Valid Movie pMovie, BindingResult pResult, #PathVariable Integer pMovieId, ModelMap pModel) {
Movie movie = _movieService.getMovieById(pMovieId);
if(pResult.hasErrors()) {
return "edit/movie";
}
//save
//redirect to moviepage
return "redirect:/movie/" + pMovieId;
}
}
and finally my movie class:
public class Movie implements Serializable{
private int _id;
#NotEmpty
#Size(min=3)
private String _title;
#NotEmpty
private String _filename;
#Pattern(regexp="tt+[0-9]{7}")
private String _imdbId;
#Min(value=1)
private int _runtime;
//getters and setters....
}
I know this question has been asked a lot of times before, but none of the answers worked with me.
thanks for your help
I solved it by renaming the attributes (removing the underlines). However it was like "jpprade" said (maybe there are accessed by reflection).
For those who can't rename the class attributes (in case of some codingconventions, ...) can annotate the getter methods.

how to get the element of a list inside jsp using JSTL?

I have such this code inside my Spring MVC java controller class:
#RequestMapping(value = "jobs", method = { RequestMethod.GET })
public String jobList(#PathVariable("username") String username, Model model) {
JobInfo[] jobInfo;
JobStatistics js;
LinkedList<JobStatistics> jobStats = new LinkedList<JobStatistics>();
try {
jobInfo = uiClient.getJobs(username);
for (int i = 0; i < jobInfo.length; i++) {
js = uiClient.getJobStatistics(jobInfo[i].getJobId());
jobStats.add(js);
}
model.addAttribute("jobs", jobInfo);
model.addAttribute("jobStats", jobStats);
}
which uiClient will get some data from database using RMI ...
now I want to show the jobs & related statistic inside my JSP file using JSTL :
<c:set var="stats" value="${jobStats}" />
<c:forEach var="jobs" items="${jobs}">
<c:set var="jobID" value="${jobs.JobId}"/>
<table>
<tr class="tr1">
<td>${jobs.Topic}</td>
<td>${stats.get(i).No}</td>
</tr>
</table>
</c:forEach>
How do I get the LinkedList elements of Model inside my JSP using JSTL? There might be no no counter i been put in scope for me.
In my opinion, the right answer is a combination of both of the answers you got:
use varStatus attribute of c:foreach tag
but:
"get" is not a jstl function.
<c:forEach var="jobs" items="${jobs}" varStatus="i">
<c:set var="jobID" value="${jobs.jobId}"/>
<table>
<tr class="tr1">
<td>${jobs.topic}</td>
<td>${stats[i.index].no}</td>
</tr>
</table>
</c:forEach>
EDIT: this is the code finally used by the author of the question:
<c:set var="stats" value="${jobStats}" />
<c:forEach items="${jobs}" varStatus="i">
<c:set var="jobID" value="${jobs[i.index].jobId}"/>
<table>
<tr class="tr1">
<td>${jobs[i.index].topic}</td>
<td>${stats[i.index].no}</td>
<td>${jobID}</td>
</tr>
</table>
</c:forEach>
get is not a jstl function.
<td>${stats[i.index].No}</td>
use varStatus attribute of c:foreach tag
<c:forEach var="jobs" items="${jobs}" varStatus="i">
<c:set var="jobID" value="${jobs.JobId}"/>
<table>
<tr class="tr1">
<td>${jobs.Topic}</td>
<td>${stats.get(i.index).No}</td>
</tr>
</table>
</c:forEach>

Resources