My first Spring MVC application, the first Spring MVC application

Source: Internet
Author: User

My first Spring MVC application, the first Spring MVC application

Product

Package com. mstf. bean; import java. io. serializable;/*** Product class, which encapsulates some information, including three attributes * @ author wangzheng **/public class Product implements Serializable {private static final long serialVersionUID = 1L; private String name; private String description; private float price; public String getName () {return name;} public void setName (String name) {this. name = name;} public String getDescription () {return description;} public void setDescription (String description) {this. description = description;} public float getPrice () {return price;} public void setPrice (float price) {this. price = price ;}}

ProductForm

Package com. mstf. bean. form;/*** ProductForm is a form class *: When data verification fails, used to save and Display User input in the original form * @ author wangzheng */public class ProductForm {private String name; private String description; private String price; public String getName () {return name;} public void setName (String name) {this. name = name;} public String getDescription () {return description;} public void setDescription (String description) {this. description = description;} public String getPrice () {return price;} public void setPrice (String price) {this. price = price ;}}

InputProductController

Package com. mstf. controller; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. apache. commons. logging. log; import org. apache. commons. logging. logFactory; import org. springframework. web. servlet. modelAndView; import org. springframework. web. servlet. mvc. controller; public class InputProductController implements Controller {private static final Log logger = LogFactory. getLog (InputProductController. class);/*** return a ModelAndView that contains a view without a model. forward all data directly */@ Overridepublic ModelAndView handleRequest (HttpServletRequest req, HttpServletResponse resp) throws Exception {logger.info ("handleRequest successful"); return new ModelAndView ("/WEB-INF/jsp/ProductForm. jsp ");}}

SaveProductController

Package com. mstf. controller; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. apache. commons. logging. log; import org. apache. commons. logging. logFactory; import org. springframework. web. servlet. modelAndView; import org. springframework. web. servlet. mvc. controller; import com. mstf. bean. product; import com. mstf. bean. form. productForm; public class SaveProductController implements Controller {private static final Log logger = LogFactory. getLog (InputProductController. class); @ Overridepublic ModelAndView handleRequest (HttpServletRequest req, HttpServletResponse resp) throws Exception {req. setCharacterEncoding ("UTF-8"); // transcode resp. setCharacterEncoding ("UTF-8"); logger.info ("successfully entering SaveProductController"); // construct a ProductForm object ProductForm productForm = new ProductForm (); // write the form object productForm. setName (req. getParameter ("name"); productForm. setDescription (req. getParameter ("description"); productForm. setPrice (req. getParameter ("price"); // create model Product product = new Product (); product. setName (productForm. getName (); product. setDescription (productForm. getDescription (); try {product. setPrice (Float. parseFloat (productForm. getPrice ();} catch (Exception e) {e. printStackTrace ();} return new ModelAndView ("/WEB-INF/jsp/ProductDetails. jsp "," product ", product );}}

ProductDetails. jsp

<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html> 

ProductForm. jsp

<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html> 

Springmvc-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"       xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans.xsd">           <bean name="/product_input.action" class="com.mstf.controller.InputProductController"/>    <bean name="/product_save.action" class="com.mstf.controller.SaveProductController"/></beans>

Web. xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"    version="3.0"> <servlet>        <servlet-name>springmvc</servlet-name>        <servlet-class>            org.springframework.web.servlet.DispatcherServlet        </servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>springmvc</servlet-name>        <url-pattern>*.action</url-pattern>    </servlet-mapping>  </web-app>

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.