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??
我在finalresult.jsp中显示name和fullname值,但是只有我得到的fullname值,在finalresult.jsp中名称值为空。请帮助我如何在finalresult.jsp页面中显示这两个值。请告诉我如何实现上面的sesion? 提前致谢 希望有人能尽快指导我
答案 0 :(得分:2)
finalResult.jsp
正在寻找名为name
的属性,但此时请求中没有名为name
的属性。
如果您想将第一个控制器RegistrationController
捕获的名称传递给第二个控制器AdditionalDetailsController
,那么最简单的方法就是使用会话。
<强>这个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";
}
然后在finalResult.jsp
中,您应该能够直接从会话中访问Student
:
<强> 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>
作为旁注 - 由于控制器的多线程特性,您不应将Student
存储在RegistrationController
的实例字段中。这应该在方法中本地创建。
@RequestMapping(method=RequestMethod.GET)
public String toRegform(ModelMap model)
{
model.addAttribute("student", new Student()); // Create student
return "student";
}
答案 1 :(得分: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>