Spring MVC Pattern Example (decoupling controller not used)

Source: Internet
Author: User

Product

Package Com.mstf.bean;import java.io.serializable;/** * Product class, encapsulates some information, contains three properties * @author Wangzheng * */public class Prod UCT implements Serializable {private static final long Serialversionuid = 1l;private string Name;private string Descriptio n;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 * function: Used to save and present user input in the original form * @author Wangzheng * */public class Prod when data validation fails Uctform {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;}}

Controllerservlet

Package Com.mstf.servlet;import Java.io.ioexception;import Javax.servlet.requestdispatcher;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import com.mstf.bean.Product Import Com.mstf.bean.form.productform;public class Controllerservlet extends HttpServlet {private static final long Serialversionuid = 1L; @Overridepublic void Doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {process (req, resp);} @Overridepublic void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException { Process (req, resp);} /** * This method is used to process all input requests * @param req * @param resp * @throws ioexception * @throws servletexception */private void Process (Ht Tpservletrequest Req,httpservletresponse resp) throws Ioexception,servletexception {req.setcharacterencoding ("UTF-8 "); transcoding resp.setcharacterencoding ("UTF-8"); String uri=req.getrequesTURI (); Get request Uriint Lastindex=uri.lastindexof ("/"); String action=uri.substring (lastindex+1); Gets the action name//execution method if (action.equals ("Product_input.action")) {//no found, no action} else if (Action.equals ("product_ Save.action ")) {//Build a Productform form object according to the request parameters Productform productform=new productform ();// Populates the Form Object Data Productform.setname (Req.getparameter ("name"));p roductform.setdescription (req.getparameter ("description "));p Roductform.setprice (" price ");//Create the product model (domain object) and set the corresponding property through the form object Product Product=new product ();p Roduct.setname (Productform.getname ());p roduct.setdescription (Productform.getdescription ()); try { Product.setprice (Float.parsefloat (Productform.getprice ()));} catch (Exception e) {e.printstacktrace ();} Put the product model into the HttpServletRequest object so that the corresponding attempt can access the Req.setattribute ("Product", product);} Forward to this view string dispatchurl=null;if (Action.equals ("Product_input.action")) {dispatchurl= "web-inf/jsp/ Productform.jsp ";} else if (action.equals ("Product_save.action")) {dispatchurl= "Web-inf/jsP/productdetails.jsp ";} if (dispatchurl!=null) {RequestDispatcher rd=req.getrequestdispatcher (dispatchurl); Rd.forward (req, resp);}}}

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> Controllerservlet</servlet-name>    <servlet-class>com.mstf.servlet.controllerservlet</ Servlet-class></servlet><servlet-mapping>    <servlet-name>controllerservlet</ servlet-name>    <url-pattern>*.action</url-pattern></servlet-mapping>  </web-app >

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>

  

Spring MVC Pattern Example (decoupling controller not used)

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.