0
    Welcome.jsp page
    <a href="addStudent">Click here for registration form</a>

    student.jsp is the registration page

    <s:form method="post" action="addStudent" commandName="student">

    <s:label path="name">NAME:</s:label>
    <s:input path="name"/><br>
    <font color="red"><s:errors path="name"></s:errors></font><br>

    result.jsp

    <table>
    <tr>
    <td><font color="red">NAME:</font></td>
    <td><font color="blue">${student.name}</font></td>
    </tr></table>

    studentmoredetails.jsp

    <s:form method="POST" action="studentadditionaldetails" commandName="studentMoreDetails">
    <s:label path="fullname">FULLNAME:</s:label>
    <s:input path="fullname"/>

    final result.jsp

    <table>
    <tr>
    <td><font color="red">NAME:</font></td>
    <td><font color="blue">${name}</font></td>
    </tr>

    <tr>
    <td><font color="red">FULLNAME:</font></td>
    <td><font color="blue">${studentMoreDetails.fullname}</font></td>
    </tr></table>

    I am having 2 controller class and two bean class

    RegistrationController.java

    @Controller
    @RequestMapping("addStudent")
    public class RegistrationController {
        Student studentobj=new Student();
        @RequestMapping(method=RequestMethod.GET)
        public String toRegform(ModelMap model)
        {                   
            model.addAttribute("student", studentobj);      
            return "student";
        }

        @RequestMapping(method = RequestMethod.POST)
        public String addStudent(@Valid Student login, BindingResult result,ModelMap model) 
        {           
            if (result.hasErrors()) {
                return "student";
            }

            login = (Student) model.get("student");
            if ((login.getName()==null))
            {
                return "student";
            }
        //  model.put("student", login);        
            return "result";        
        }       

    Student.java (bean class)
    private int age;
        @NotEmpty
        @Size(min=3,max=15)
        private String name;    
    // getters and setters

    AdditionaldetailsController.java
    @Controller
    @RequestMapping("studentadditionaldetails")
    public class AdditionalDetailsConroller {
        @RequestMapping(method=RequestMethod.GET)
        public String additionalForl(ModelMap model1)
        {       
            StudentMoreDetails additional=new StudentMoreDetails();
            model1.addAttribute("studentMoreDetails", additional);
            return "studentmoredetails";
        }
        @RequestMapping(method=RequestMethod.POST)  
        public String additionalDetails(@Valid StudentMoreDetails login,BindingResult result,ModelMap model1)
        {       
            if (result.hasErrors()) 
            {
                return "studentmoredetails";
            }
            login=(StudentMoreDetails) model1.get("studentMoreDetails");        
            if (login.getFullname()==null)
            {
                return "studentmoredetails";
            }
            else
            {
                return "finalresult";
            }
        }
    }
     StudentMoreDetails.java(bean class)
    private String fullname;
    //getters and setters

    web.xml file 

    <servlet>
        <servlet-name>springdispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>

      <servlet-mapping>
        <servlet-name>springdispatcher</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>

    springdispatcher-servlet.xml file is
    <mvc:annotation-driven/>
    <context:component-scan base-package="org.pratap.javashades.controllers"></context:component-scan>

    <bean id="viewResolver"

            class="org.springframework.web.servlet.view.InternalResourceViewResolver">

            <property name="prefix" value="/WEB-INF/JSP/"></property>

            <property name="suffix" value=".jsp"></property>
    </bean>

    <bean id="messageSource"

            class="org.springframework.context.support.ReloadableResourceBundleMessageSource">

            <property name="basename" value="/WEB-INF/messages" />

    </bean>     

    </beans>

    i am displaying both name and fullname values in the finalresult.jsp, but only fullname value i am getting, name value is empty in finalresult.jsp. please help me how can i display both the values in finalresult.jsp page. Please tell me how to achieve sesion for above??

i am displaying both name and fullname values in the finalresult.jsp, but only fullname value i am getting, name value is empty in finalresult.jsp. please help me how can i display both the values in finalresult.jsp page. Please tell me how to achieve sesion for above?? Thanks in advance Hope somebody will guide me soon

2 Answers2

2

The finalResult.jsp is looking for an attribute called name, but there is no attribute called name available in the request at that point.

If you want to pass the name that was captured by the first controller RegistrationController to the second controller AdditionalDetailsController then it may be easiest just to use the session for this.

RegistrationController

    @RequestMapping(method = RequestMethod.POST)
    public String addStudent(HttpSession session, @Valid Student login, BindingResult result,ModelMap model) 
    {           
        if (result.hasErrors()) {
            return "student";
        }

        login = (Student) model.get("student");
        if ((login.getName()==null))
        {
            return "student";
        }

        session.setAttribute("student", login); // Set the Student in session

    //  model.put("student", login);        
        return "result";        
    }       

Then in finalResult.jsp you should be able to access the Student directly from the session:

finalResult.jsp

<table>
<tr>
<td><font color="red">NAME:</font></td>
<td><font color="blue">${sessionScope.student.name}</font></td>
</tr>

<tr>
<td><font color="red">FULLNAME:</font></td>
<td><font color="blue">${studentMoreDetails.fullname}</font></td>
</tr></table>

As a side note - you should not store the Student in an instance field in RegistrationController due to the multi-threaded nature of controllers. This should be created locally within the method.

    @RequestMapping(method=RequestMethod.GET)
    public String toRegform(ModelMap model)
    {                   
        model.addAttribute("student", new Student()); // Create student
        return "student";
    }
Will Keeling
  • 22,055
  • 4
  • 51
  • 61
  • The OP should probably use a single model but I like this solution for addressing the immediate problem – andyb Dec 16 '13 at 16:05
  • thank you very much Will Keeling, I have waited almost 15 days to know about this. Thank you again –  Dec 17 '13 at 05:02
0
add these lines to your controller class

@SessionAttributes({"student"})
public class RegistrationController {
.
.
.
}

@SessionAttributes({"student"})
public class AdditionalDetailsConroller {

@RequestMapping(method=RequestMethod.POST)  
    public String additionalDetails(@Valid StudentMoreDetails login,BindingResult result,ModelMap model1,@ModelAttribute Student student)
    {   
        System.out.println("stu"+student.getName());
        if (result.hasErrors()) 
        {
            return "studentmoredetails";
        }
        login=(StudentMoreDetails) model1.get("studentMoreDetails");        
        if (login.getFullname()==null)
        {
            return "studentmoredetails";
        }
        else
        {
            return "finalresult";
        }
    }   
}

finalresult.jsp will be
<table>
<tr>
<td><font color="red">NAME:</font></td>
<td><font color="blue">${student.name}</font></td>
</tr>

<tr>
<td><font color="red">FULLNAME:</font></td>
<td><font color="blue">${studentMoreDetails.fullname}</font></td>
</tr>
</table>
Pratap A.K
  • 4,337
  • 11
  • 42
  • 79