0% found this document useful (0 votes)
91 views5 pages

Spring MVC SimpleFormController Example

This document provides an example of using Spring MVC with a SimpleFormController class to create a web application that accepts user input for a product form, saves the input to a Product model object, and displays the saved product details on a subsequent page. Key aspects include: - Index JSP redirects to the new product form - Web.xml configures the DispatcherServlet and maps URLs to the productaction servlet - The servlet configuration defines a controller bean for the form, maps the URL, and specifies form and success views - JSPs define the input form and output page using Spring form tags bound to the model object - A Product Java class defines the model object - The

Uploaded by

kalyanbrp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views5 pages

Spring MVC SimpleFormController Example

This document provides an example of using Spring MVC with a SimpleFormController class to create a web application that accepts user input for a product form, saves the input to a Product model object, and displays the saved product details on a subsequent page. Key aspects include: - Index JSP redirects to the new product form - Web.xml configures the DispatcherServlet and maps URLs to the productaction servlet - The servlet configuration defines a controller bean for the form, maps the URL, and specifies form and success views - JSPs define the input form and output page using Spring form tags bound to the model object - A Product Java class defines the model object - The

Uploaded by

kalyanbrp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Spring MVC:

Example: Spring MVC example with SimpleFormController class


<!-- index.jsp -->
<jsp:forward page="/newpro.htm" />

<!-- web.xml -->


<web-app>
<servlet>
<servlet-name>productaction</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>productaction</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>spring-tld</taglib-uri>
<taglib-location>/WEB-INF/spring.tld</taglib-location>
</taglib>
</web-app>

<!-- productaction-servlet.xml -->


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="simpleUrlMappings"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping>

<property name="mappings">
<props>
<prop key="/newpro.htm">newproController</prop>
</props>
</property>
</bean>
<bean id="newproController" class="NewProductController" >
<property name="sessionForm" > <value>true</value></property>
<property name="commandName" > <value>product</value></property>
<property name="commandClass" > <value>Product</value></property>
<property name="formView" > <value>newproduct</value></property>
<property name="successView" > <value>showproduct</value></property>

</bean>

<bean id="viewResolver" class =


"org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name ="prefix"><value>/</value></property>
<property name ="suffix"><value>.jsp</value></property>
</bean>
</beans>

<!-- newproduct.jsp -->


<%@ page language="java" %>
<%@ taglib prefix="spring" uri="spring-tld" %>
<html>
<body bgcolor=#ffffcc text=green>
<h1><center>new product information</h1><hr><br><h3>
<form method="post" >
enter product id :
<spring.bind path="product.pid" >

<input type=text name=pid />


</spring.bind>
<br><br>
enter product name :
<spring.bind path="product.pname" >
<input type=text name=pname />
</spring.bind>
<br><br>
enter product price :
<spring.bind path="product.price" >
<input type=text name=price />
</spring.bind>
<br><br>
<input type=submit value=send />
</form>
</body>
</html>
//Product.java
public class Product
{
private int pid;
private String pname;
private double price;
public void setPid(int n){ pid=n; }
public int getPid(){ return pid; }
public void setPname(String s){ pname=s; }
public String getPname(){ return pname; }
public void setPrice(double p){ price=p; }
public double getPrice(){ return price; }
}

//NewProductController.java
import org.springframework.web.servlet.mvc.*;
import org.springframework.web.servlet.*;
import org.springframework.validation.*;
import javax.servlet.http.*;

public class NewProductController extends SimpleFormController


{
public ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse
response,Object command,BindException e) throws Exception
{
Product p=(Product)command;
String str="Product id :"+p.getPid()+" <br> "
+"product name :"+p.getPname()+"<br> "
+"Price :"+p.getPrice();
System.out.println(str);
request.setAttribute("productinfo",str);
return new ModelAndView(getSuccessView());
// return new ModelAndView("showproduct");

<!-- showproduct.jsp -->


<%@ page language="java" %>
<html>
<body bgcolor=#ffffcc text=green>
<h1><center>new product information</h1><hr><br><h3>
<%= request.getAttribute("productinfo") %>
</body>
</html>

You might also like