zoukankan      html  css  js  c++  java
  • Spring MVC模式示例(未采用解耦控制器)

    Product

    package com.mstf.bean;
    
    import java.io.Serializable;
    /**
     * Product类,封装了一些信息,包含三个属性
     * @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是表单类
     * 作用:当数据校验失败时,用于保存和展示用户在原始表单的输入
     * @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;
    	}
    	
    }
    

      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;
    
    	@Override
    	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    		process(req, resp);
    	}
    
    	@Override
    	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    		process(req, resp);
    	}
    	/**
    	 * 这个方法用来处理所有输入请求
    	 * @param req
    	 * @param resp
    	 * @throws IOException
    	 * @throws ServletException
    	 */
    	private void process(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException {
    		req.setCharacterEncoding("UTF-8"); //转码
    		resp.setCharacterEncoding("UTF-8");
    		String uri=req.getRequestURI(); // 获取请求URI
    		int lastIndex=uri.lastIndexOf("/");
    		String action=uri.substring(lastIndex+1); // 获取action名称
    		// 执行方法
    		if(action.equals("product_input.action")) {
    			// 没有找到,不进行任何操作
    		} else if (action.equals("product_save.action")) {
    			// 根据请求参数构建一个ProductForm表单对象 
    			ProductForm productForm=new ProductForm();
    			// 填充表单对象数据
    			productForm.setName(req.getParameter("name"));
    			productForm.setDescription(req.getParameter("description"));
    			productForm.setPrice(req.getParameter("price"));
    			
    			// 创建Product模型(领域对象),并通过表单对象设置相应属性
    			Product product=new Product();
    			product.setName(productForm.getName());
    			product.setDescription(productForm.getDescription());
    			try {
    				product.setPrice(Float.parseFloat(productForm.getPrice()));
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    			// 将Product模型放入HttpServletRequest对象中,以便对应的试图可以访问
    			req.setAttribute("product", product);
    		}
    		
    		// 转发到这个视图
    		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);
    		}
    	}
    }
    

      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>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>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>详情</title>
    <style type="text/css">@IMPORT url("css/main.css");</style>
    </head>
    <body>
    	<div id="global">
        <h4>产品已保存</h4>
        <p>
            <h5>详细列表:</h5>
            	名称: ${product.name}<br>
            	简介: ${product.description}<br>
            	价格: ¥${product.price}
        </p>
    </div>
    </body>
    </html>
    

      ProductForm.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>添加</title>
    <style type="text/css">@IMPORT url("css/main.css");</style>
    </head>
    <body>
    	<div id="global">
    		<form action="product_save.action" method="post">
    			<fieldset>
    				<legend>添加:</legend>
    					<p>
    					    <label for="name">名称: </label>
    						<input type="text" id="name" name="name" tabindex="1">
    		            </p>
    		            <p>
    					    <label for="description">简介: </label>
    						<input type="text" id="description" name="description" tabindex="2">
    					</p>
    		            <p>
    		    			<label for="price">价格: </label>
    						<input type="text" id="price" name="price" tabindex="3">
    					</p>
    					<p id="buttons">
    						<input id="reset" type="reset" tabindex="4">
    						<input id="submit" type="submit" tabindex="5" value="添加">
    					</p>
    			</fieldset>
    		</form>
    	</div>
    </body>
    </html>
    

      

  • 相关阅读:
    python实现图像仿射变换 以图像缩放并平移为例讲解
    图像仿射变换之图像平移 python实现
    图解图像仿射变换
    2-Maven
    1-IDEA
    公开密钥算法-RSA算法
    公开密钥算法-背包算法
    对称密钥算法
    Java内存分配与参数传递
    Oracle怎么用(常用工具)
  • 原文地址:https://www.cnblogs.com/ceet/p/6593637.html
Copyright © 2011-2022 走看看