'Getting an Error in JSP page as well as controller
I am getting an error in my JSP page when I am trying to run my spring web page. Errors are:
An exception occurred processing [jsp/welcome.jsp] at line [42]
org.apache.jasper.JasperException: An exception occurred processing [jsp/welcome.jsp] at line [42]
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'question' available as request attribute
One different error is when I am login to my web app it still shows
http://localhost:8080/caseStudyNew/logininstead ofhttp://localhost:8080/caseStudyNew/welcomeorhttp://localhost:8080/caseStudyNew/login/welcome
below is code of JSP page and controller
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859 1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome</title>
<style>
* {
font-family: cursive;
}
body {
background-color: rgb(70, 187, 233);
}
</style>
</head>
<body>
<tr>
<td>Welcome ${firstname}</td>
</tr>
<br>
<tr>
<td><a href="home.jsp">Home</a></td>
</tr>
<h1 class="heading">Welcome</h1>
<h2 class="heading">Problem shared is a problem solved!</h2>
<form:form id="quesForm" modelAttribute="question" formactiom="question" method="post">
<table>
<tr>
<td>
<form:label path="ques">Enter Your Question</form:label>
</td>
<td>
<form:input path="ques" name="ques" id="ques" />
</td>
</tr>
<tr>
<td>
<form:label path="ques_desc">Add the Description</form:label>
</td>
<td>
<form:input path="ques_desc" name="ques_desc" id="ques_desc" />
</td>
</tr>
<tr>
<td>
<form:button id="ques_submit" name="submit">Ask!</form:button>
</td>
</tr>
</table>
</form:form>
</html>
Controller Code:
package jbr.springmvc.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import jbr.springmvc.model.Question;
import jbr.springmvc.service.UserService;
@Controller
public class QuestionController {
@Autowired
public UserService userService;
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public ModelAndView showRegister(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView("welcome");
mav.addObject("question", new Question());
return mav;
}
@RequestMapping(value = "/welcome", method = RequestMethod.POST)
public ModelAndView addUser(HttpServletRequest request, HttpServletResponse response,
@ModelAttribute("question") Question question) {
userService.question(question);
return new ModelAndView("Hey your", "question", question.getQues());
}
}
Model Code
package jbr.springmvc.model;
public class Question {
private int quesid;
private String ques;
private String ques_desc;
public int getQuesid() {
return quesid;
}
public void setQuesid(int quesid) {
this.quesid = quesid;
}
public String getQues() {
return ques;
}
public void setQues(String ques) {
this.ques = ques;
}
public String getQues_desc() {
return ques_desc;
}
public void setQues_desc(String ques_desc) {
this.ques_desc = ques_desc;
}
}
Solution 1:[1]
Try creating no-arg constructor for the Question class. public Question(){ }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
