hi
Trying to build my own spring application,I made a page to input name and age.Clicking on Submit button should take me to another page that displays the user input.I decided not to use any database.As the first step ,I decided to create a controller with a method to show the form view.After I get this right,I would implement the other methods..
package personinfo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import personinfo.form.PersonInfo;
@Controller
public class PersonInfoController {
@RequestMapping(value ="/addPerson" )
public ModelAndView showAddPersonForm(){
return new ModelAndView("addperson", "personInfoEntries", new PersonInfo());
}
@RequestMapping(value ="/addPerson" ,method =RequestMethod.POST)
public String addPersonInfo(@ModelAttribute("person")PersonInfo perInfo){
String name = perInfo.getName();
int age = perInfo.getAge();
System.out.println("Name:" + name +"Age:"+ age);
return "showperson";//how to pass name and age
}
...
}
form backing object is personinfo.form.PersonInfo class
package personinfo.form;
public class PersonInfo {
private String name;
private int age;
public PersonInfo(){
}
public PersonInfo(String name,int age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
/WEB-INF/jsp/addperson.jsp
<%@ 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">
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add Person</title>
</head>
<body>
<h2>Add Person</h2>
<form:form action="addPerson.html" commandName="personInfoEntries">
<table>
<tr>
<td>
<form:label path="name">Name</form:label>
</td>
<td>
<form:input path="name"/>
</td>
</tr>
<tr>
<td>
<form:label path="age">Age</form:label>
</td>
<td>
<form:input path="age"/>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Add Person"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
the perinfo/WebContent/index.jsp is
<%@ 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>index</title>
</head>
<body>
<jsp:forward page="addPerson.html"></jsp:forward>
</body>
</html>
/perinfo/WebContent/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>personinfo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
finally the /perinfo/WebContent/WEB-INF/dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<mvc:annotation-driven/>
<context:annotation-config />
<context:component-scan base-package="personinfo.controller" />
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp">
<property name="contentType" value="text/html; charset=utf-8" />
</bean>
</beans>
As chris pointed out,I added the @Controller to controller class and to the servlet.xml .
Now I am able to see the form.When I click on the submit button ,the showperson.jsp is displayed.So far so good.
When the addPersonInfo() method in PersonInfoController is executed I would like to pass the name and age values to showperson.jsp .How can I do this? I am returning a ‘showperson’ view name string.How can I add the arguments to it?And I couldn’t work out how to implement a corresponding method to which this viewname is to be mapped.
Can someone please show me?Or do I need to modify the return types of addPersonInfo() method.
Please help.
thanks,
mark
To answer your follow-up question,
You need to add the name and age values to the model and pass that to the view. You do it inside addPerson().
Then in your JSP page, you should at least have something like this:
Check my other tutorials on the link I provided. There’s plenty of examples like that