热卖商品
新闻详情
springMVC入门(三) - vastshine
来自 : vastshine.com/article/detail/.
发布时间:2021-03-25
public class Product implements Serializable { private static final long serialVersionUID = 123456789L; private String name; private String description; private BigDecimal 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 BigDecimal getPrice() { return price; public void setPrice(BigDecimal price) { this.price = price;
ProductForm.java
package com.model;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;
ProductService.java
package com.service;import com.model.Product;public interface ProductService { Product add(Product product); Product get(int id);
ProductServiceImpl.java
package com.service;import java.math.BigDecimal;import java.util.HashMap;import java.util.Map;import java.util.concurrent.atomic.AtomicLong;import org.springframework.stereotype.Service;import com.model.Product;import com.service.ProductService;public class ProductServiceImpl implements ProductService { private Map Long, Product products = new HashMap Long, Product private AtomicLong generator = new AtomicLong(); public ProductServiceImpl() { Product product = new Product(); product.setName( 产品1 product.setDescription( 产品简介 product.setPrice(new BigDecimal(39.99)); add(product); @Override public Product add(Product product) { long newId = generator.incrementAndGet(); product.setId(newId); products.put(newId, product); return product; @Override public Product get(long id) { return products.get(id);
ProductController.java
package com.mycontroller;import java.math.BigDecimal;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import com.model.Product;import com.model.ProductForm;@Controllerpublic class ProductController { private static final Log logger = LogFactory.getLog(ProductController.class); @RequestMapping(value= /input-product ) public String inputProduct() { logger.info( inputProduct called return ProductForm @RequestMapping(value= /save-product ) public String saveProduct(ProductForm productForm, Model model) { logger.info( saveProduct called // no need to create and instantiate a ProductForm // create Product Product product = new Product(); product.setName(productForm.getName()); product.setDescription(productForm.getDescription()); try { product.setPrice(new BigDecimal(productForm.getPrice())); } catch (NumberFormatException e) { // add product model.addAttribute( product , product); return ProductDetails
ProductForm.jsp
%@ page contentType= text/html;charset=UTF-8 language= java % html head title Product Form /title /head body form action= save-product 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 Description: /label input type= text id= description name= description tabindex= 2 label for= price Price: /label input type= text id= price name= price tabindex= 3 input id= reset type= reset tabindex= 4 input id= submit type= submit tabindex= 5 /fieldset /form /body /html
ProductDetails.jsp
%@ page contentType= text/html;charset=UTF-8 language= java % html head title Product Details /title /head body div h4 The product has been saved. /h4 h5 Details: /h5 Product Name: ${product.name} br/ Description: ${product.description} br/ Price: $${product.price} /div /body /html
浏览器访问http://localhost:8080/input-product
下面使用@Autowired和@Service进行依赖注入
dispatcher-servlet.xml添加一项
!--service-- context:component-scan base-package= com.service /
ProductServiceImpl.java加入@Service
@Servicepublic class ProductServiceImpl implements ProductService {
ProductController.java修改如下
package com.mycontroller;import java.math.BigDecimal;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import com.model.Product;import com.model.ProductForm;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.servlet.mvc.support.RedirectAttributes;import org.springframework.web.bind.annotation.PathVariable;import com.service.ProductService;
本文链接: http://vastspring.immuno-online.com/view-744611.html
发布于 : 2021-03-25
阅读(0)
最新动态
2021-03-25
2021-03-25
2021-03-25
2021-03-25
2021-03-25
2021-03-25
2021-03-25
2021-03-25
2021-03-25
2021-03-25
2021-03-25
2021-03-25