Announcement Announcement Module
Collapse
No announcement yet.
How to pass attributes from between methods in controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to pass attributes from between methods in controller

    Hello dear Spring community.

    First: I need to pass attribute from form to method in controller.

    my code:

    Code:
    @Controller
    @SessionAttributes("creationDate")
    public class HomeController extends HttpServlet{
    		
    	
    	private static final long serialVersionUID = 4825408935018763217L;
    
    	@SuppressWarnings("unused")
    	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);	 
    	
    	@Autowired
    	private ControllerSupportClass controllerSupportClass; 
    	
    	
    		public void setControllerSupportClass(
    				ControllerSupportClass controllerSupportClass) {
    			this.controllerSupportClass = controllerSupportClass;
    		}
    	
    
    		@ModelAttribute("creationDate")
    		public CreationDate addSessionAttrubutes() {
    			return new CreationDate();
    			
    			
    		}
    		
    		@RequestMapping(value ="/index", method=RequestMethod.GET)
    		public ModelAndView firstPage(Model model) {
    			
    			model.addAttribute("creationDate");
    						
    			return new ModelAndView("index");
    		}
    		
    		@RequestMapping(value="/add", method=RequestMethod.GET)
    		public String addingData(@ModelAttribute("creationDate") CreationDate creationDate, BindingResult result, Model model) {
    			
    			model.addAttribute("creationDate", creationDate);
    			
    			/*controllerSupportClass.saveDataToDataBase(creationDate);*/
    			
    			return "add";
    			
    		}
    
    }
    My form jsp:

    Code:
    <html>
    
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Страница выборки</title>
    </head>
    
    <body>
    
    <h3>Вставка данных:</h3>
    
    <form:form commandName="creationDate" method="GET" action="add">
    
    <form:label path="particularDate">Particular Date</form:label>
    <form:input path="particularDate" /> <br>
    
    <form:label path="childGoSchoolDate">Child go to School</form:label>
    <form:input path="childGoSchoolDate"/> <br>
    
    
    <form:label path="childAdmissionDate">Child admission Date</form:label>
    <form:input path="childAdmissionDate"/> <br>
    
    
    <input type="submit" value="Save"/>
    
    </form:form>
    
    <c:if test="${not empty creationDate}">
    
       <c:out value="${creationDate.particularDate}"/>
       <c:out value="${creationDate.childGoSchoolDate}"/>
        <c:out value="${creationDate.childAdmissionDate}"/>
        
    </c:if>
    
    </body>
    
    </html>
    My result jsp the data I'm passing should appear on this page:

    Code:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Insert title here</title>
    </head>
    <body>
    
    <h1>Result:</h1>
    
      Particular Date: <c:out value="${creationDate.particularDate}"/>
      Child go to school: <c:out value="${creationDate.childGoSchoolDate}"/>
      Child admission date:  ${creationDate.childAdmissionDate}
    
    
    </body>
    </html>
    The second problem I'm actualy don't want to have method in controller just to render my index.jsp. But I tried to put <welcome-page> to web.xml but it's doesent work. Could you help me with this. Thank you.

  • #2
    First you don't need a SessionAttribute for that, you can simply recreate the object each time. Next why on earth is your controller extending HttpServlet?! It is a controller not a servlet...

    The second problem I'm actualy don't want to have method in controller just to render my index.jsp. But I tried to put <welcome-page> to web.xml but it's doesent work. Could you help me with this. Thank you.
    You cannot have that, if you let the servlet container select the jsp no model is going to be prepared and basically render the form tags useles.

    I would suggest modifying your controller

    Code:
    @Controller
    public class HomeController {
    	
    	private static final long serialVersionUID = 4825408935018763217L;
    
    	@SuppressWarnings("unused")
    	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);	 
    	
    	@Autowired
    	private ControllerSupportClass controllerSupportClass; 
    	
    	public void setControllerSupportClass(ControllerSupportClass controllerSupportClass) {
    		this.controllerSupportClass = controllerSupportClass;
    	}
    
    	@ModelAttribute("creationDate")
    	public CreationDate addSessionAttrubutes() {
    		return new CreationDate();
    	}
    	
    	@RequestMapping(value ="/index", method=RequestMethod.GET)
    	public void index() {}
    	
    	@RequestMapping(value="/add", method=RequestMethod.POST)
    	public String addingData(@ModelAttribute("creationDate") CreationDate creationDate, BindingResult result, Model model) {
    		model.addAttribute("creationDate", creationDate);
    		controllerSupportClass.saveDataToDataBase(creationDate);
    		return "add";		
    	}
    }
    I also would suggest using POST to submit the data, modifying data with a GET request is a bad practice. Also I suspect, due to the GET request, that the addingData method is executed twice, resulting in an empty model attribute.

    Comment


    • #3
      Thank you sir for your replay... I'm very new in Spring MVC. If it will not be so roughly, I would like to ask you:
      - I think how is suppose to render my index.jsp if I will call /index on public void index() method, because it's nothing say that it has to render my index.jsp page???
      - Yeah I suspect that it's render nothing when I fill up the form, why??
      - What is the difference between controller and servlet?
      I'm sorry if it is to much.

      Comment


      • #4
        - I think how is suppose to render my index.jsp if I will call /index on public void index() method, because it's nothing say that it has to render my index.jsp page???
        This is default, if no result is returned it takes a look at the URL and tries the last part as a view name, in your case index.

        I strongly suggest you read the reference guide and pickup a book about Spring (MVC). (See my signature for a Spring MVC one).

        Comment


        • #5
          Yes sir. Thank you for your help. I tried to read reference guide start form 16. Web MVC framework but it's more for experienced person, besides is no example's there, but any way to much new for me. I pucked up "Pro Spring MVC and WebFlow" it's good book may be, but kinda hard to read for non English speaker.

          Comment

          Working...
          X