Announcement Announcement Module
Collapse
No announcement yet.
Problem viewing everything on one .jsp page Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem viewing everything on one .jsp page

    Hi all,

    I have faced a problem in viewing my .jsp page. I want that submitted information from form to be displayed below form in same page like in this picture Attachment but I am getting an exeption "Neither BindingResult nor plain target object for bean name 'comment' available as request attribute". In most examples in internet submitted text is shown in other .jsp file like "success.jsp", but it is not what I want. I want that inserted text to be shown on form page below form and let user to put in another some text and so on. Problem is that I don't get it how to do this everything on one .jsp page (something like comments section on web page). I know I have to display jsp file through GET method but how..

    My Controller:
    Code:
    @Controller
    public class CommentController {
    	
    	private CommentService commentService;
    	private CommentValidator commentValidator;
    	
    	@Autowired
    	public CommentController(CommentService commentService, CommentValidator commentValidator) {
    		this.commentService = commentService;
    		this.commentValidator = commentValidator;
    	}
    	
    	@RequestMapping(value = "/main", method = RequestMethod.GET)
    	public String listComments(Map<String, Object> map) {
    		
    		map.put("comment", new Comment());
    		map.put("commentList", commentService.listComment());
    
    		return "comment";
    	}
    
    	@RequestMapping(value = "/addComment", method = RequestMethod.POST)
    	public String addComment(@ModelAttribute("comment") Comment comment, BindingResult result) {
    				
    		commentValidator.validate(comment, result);
    		if (result.hasErrors()) {
    			return "comment";
    		} else {
    			commentService.addComment(comment);
    			return "redirect:/main";
    		}
    	}
    
    }
    My comment.jsp
    Code:
    <html>
    <head>
    	<title>Komentarai</title>
    		<style>
    			.error {
    			color: #ff0000;
    			font-style: italic;
    			}
    		</style>
    </head>
    <body>
    
    <form:form action="/addComment" commandName="comment" method="post">
    
    <table>
    	<tr>Komentaras:</tr>
    	<tr>
    		<td><form:input path="komentaras" size="35" /></td>
    		<td><input type="submit" value="Prideti komentara"/></td>
    	</tr>
    	<tr>
    		<td><form:errors path="komentaras" cssClass="error" /></td>
    	</tr>
    </table>	
    </form:form>
    
    <h4>Issaugoti komentarai</h4>
    <c:if  test="${!empty commentList}">
    <table>
    <c:forEach items="${commentList}" var="comment">
    	<tr>
    		<td><c:out value="${comment.komentaras}"/></td>
    	</tr>
    </c:forEach>
    </table>
    </c:if>
    
    
    </body>
    </html>
    Can you suggest me how to do this..
    Attached Files

  • #2
    Hello am not expert to answer this kind of questions but i will give you suggetion better to use ajax scrip in one.jsp page you will get the environment what you are shown in the image file.

    Comment


    • #3
      I have to do this only with Spring. Any thoughts what I am doing wrong...

      Comment


      • #4
        Change your method from:

        Code:
        @RequestMapping(value = "/main", method = RequestMethod.GET)
        public String listComments(Map<String, Object> map) {
        		
        	map.put("comment", new Comment());
        	map.put("commentList", commentService.listComment());
        
        	return "comment";
        }
        to

        Code:
        @RequestMapping(value = "/main", method = RequestMethod.GET)
        public String listComments(Model model) {
        	
        	model.addAttribute("comment", new Comment());
        	model.addAttribute("commentList", commentService.listComment());
        
        	return "comment";
        }

        Comment

        Working...
        X