spring-mvc 设计模型

本文详细介绍了一个使用SpringMVC框架的实战案例,从部署描述符(web.xml)到控制器类的实现,再到视图解析器和产品信息的处理流程,包括输入、保存和显示产品信息的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 DispatcherServlet

调度员servlet

org.springframework.web.servlet.DispatcherServlet

部署描述符(web.xml):

<?xml version ="1.0" encoding="UTF-8" ?>
<web-app version="3.0"
         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">
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/springmvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
</web-app>

它会寻找应用程序的配置文件:servletName-servlet.xml

声明两个控制器类  Controller,对应不同的逻辑

<?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.controller.InputProductController"/>

    <bean name="/product_save.action"
        class="com.controller.SaveProductController"/>


</beans>

Product 类:

package com.domain;

import java.io.Serializable;

public class Product implements Serializable {
    private static final long serivalVersionUID = 78963434L;
    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.form;

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;
    }
}

 

视图解析器:

springmvc-config.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.controller.InputProductController"/>
    <bean name="/product_save.action" class="com.controller.SaveProductController"/>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

输入产品信息:

package com.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);

    @Override
    public ModelAndView handleRequest(HttpServletRequest request,
                                      HttpServletResponse response)throws Exception{
        logger.info("InputProductController Called");
        return new ModelAndView("ProductForm");
    }

}

保存产品信息:

package com.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.domain.Product;
import com.form.ProductForm;

public class SaveProductController implements Controller{
    private static final Log logger = LogFactory.getLog(SaveProductController.class);

    @Override
    public ModelAndView handleRequest(HttpServletRequest request,
                                      HttpServletResponse response)throws Exception{
        logger.info("SaveProductController Called");
        ProductForm productFrom = new ProductForm();

        //populate action properties
        productFrom.setName(request.getParameter("name"));
        productFrom.setDescription(request.getParameter("description"));
        productFrom.setPrice(request.getParameter("price"));

        //create model
        Product product = new Product();
        product.setName(productFrom.getName());
        product.setDescription(productFrom.getDescription());
        try{
            product.setPrice(Float.parseFloat(productFrom.getPrice()));
        }catch (NumberFormatException e){

        }

        return new ModelAndView("ProductDetails","product",product);

    }


}

显示产品信息网页:ProductDetails.jsp

<! DOCTYPE HTML>
<html>
<head>
    <title>Save Product</title>
    <style type="text/css">@import url(css/main.css);</style>
</head>
<body>
<div id="global">
    <h4>The Product has been Saved.</h4>
    <p>
        <h5>Details:</h5>
        Product Name: ${product.name}<br/>
        Description: ${product.description}<br/>
        Price:${product.price}
    </p>
</div>
</body>
</html>

输入产品信息网页:ProductForm.jsp

<! DOCTYPE HTML>
<html>
    <head>
    <title>Add Product Form</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>Add a Product</legend>
                <label for="name">Product Name:</label>
                <input type="text" id="name" name = "name"  tabindex="1">
                <label for="description">Des:</label>
                <input type="text" id="description" name="description" tabindex="2">
                <label for="price">Price:</label>
                <input type="text" id="price" name="price"  tabindex="3">
                <div id="buttons">
                    <label for="dummy"></label>
                    <input id="reset" type="reset" tabindex="4">
                    <input id="submit" type="submit" tabindex="5" value="Add Product">
                </div>
            </fieldset>
        </form>
    </div>

    </body>
</html>

 

 

 

依赖库

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值