Skip to content

Commit eff8555

Browse files
committed
spring-mvc chapter 4
1 parent 05172f5 commit eff8555

File tree

10 files changed

+95
-33
lines changed

10 files changed

+95
-33
lines changed

spring-mvc/java-spring/pom.xml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.spring</groupId>
55
<artifactId>java-spring</artifactId>
66
<packaging>war</packaging>
7-
<version>1.0-SNAPSHOT</version>
7+
<version>1.0.0</version>
88
<name>java-spring Maven Webapp</name>
99
<url>http://maven.apache.org</url>
1010

@@ -17,7 +17,7 @@
1717
<scope>test</scope>
1818
</dependency>
1919

20-
<!--3.Servlet web相关依赖 -->
20+
<!--1.Servlet web相关依赖 -->
2121
<dependency>
2222
<groupId>taglibs</groupId>
2323
<artifactId>standard</artifactId>
@@ -28,18 +28,13 @@
2828
<artifactId>jstl</artifactId>
2929
<version>1.2</version>
3030
</dependency>
31-
<dependency>
32-
<groupId>com.fasterxml.jackson.core</groupId>
33-
<artifactId>jackson-databind</artifactId>
34-
<version>2.5.4</version>
35-
</dependency>
3631
<dependency>
3732
<groupId>javax.servlet</groupId>
3833
<artifactId>javax.servlet-api</artifactId>
3934
<version>3.1.0</version>
4035
</dependency>
4136

42-
<!--4:spring依赖 -->
37+
<!--2:spring依赖 -->
4338
<!--1)spring核心依赖 -->
4439
<dependency>
4540
<groupId>org.springframework</groupId>
@@ -84,8 +79,8 @@
8479
<artifactId>spring-test</artifactId>
8580
<version>4.1.7.RELEASE</version>
8681
</dependency>
87-
8882
</dependencies>
83+
8984
<build>
9085
<finalName>java-spring</finalName>
9186
</build>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.spring.controller;
2+
3+
import java.math.BigDecimal;
4+
5+
import org.apache.commons.logging.Log;
6+
import org.apache.commons.logging.LogFactory;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.ui.Model;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
11+
import com.spring.domain.Product;
12+
import com.spring.from.ProductForm;
13+
14+
@Controller
15+
public class ProductController
16+
{
17+
private static final Log logger = LogFactory.getLog(ProductController.class);
18+
19+
@RequestMapping(value = "/input-product")
20+
public String inputProduct()
21+
{
22+
logger.info("inputProduct called");
23+
return "ProductForm";
24+
}
25+
26+
@RequestMapping(value = "/save-product")
27+
public String saveProduct(ProductForm productForm, Model model)
28+
{
29+
logger.info("saveProduct called");
30+
// no need to create and instantiate a ProductForm
31+
// create Product
32+
Product product = new Product();
33+
product.setName(productForm.getName());
34+
product.setDescription(productForm.getDescription());
35+
36+
try
37+
{
38+
product.setPrice(new BigDecimal(productForm.getPrice()));
39+
}
40+
catch (NumberFormatException e)
41+
{
42+
throw new NumberFormatException("Failed to set price." + e);
43+
}
44+
45+
// add product
46+
47+
model.addAttribute("product", product);
48+
return "ProductDetails";
49+
}
50+
}

spring-mvc/java-spring/src/main/java/com/spring/controller/SaveProductController.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.spring.controller;
22

3+
import java.math.BigDecimal;
4+
35
import javax.servlet.http.HttpServletRequest;
46
import javax.servlet.http.HttpServletResponse;
57

@@ -30,11 +32,11 @@ public ModelAndView handleRequest(HttpServletRequest request, HttpServletRespons
3032
product.setDescription(productForm.getDescription());
3133
try
3234
{
33-
product.setPrice(Float.parseFloat(productForm.getPrice()));
35+
product.setPrice(new BigDecimal(productForm.getPrice()));
3436
}
3537
catch (NumberFormatException e)
3638
{
37-
throw new NumberFormatException("Failed to set price.");
39+
throw new NumberFormatException("Failed to set price." + e);
3840
}
3941

4042
// insert code to save Product

spring-mvc/java-spring/src/main/java/com/spring/domain/Product.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
package com.spring.domain;
22

33
import java.io.Serializable;
4+
import java.math.BigDecimal;
45

56
public class Product implements Serializable
67
{
78

89
private static final long serialVersionUID = 2519593427357947906L;
910

11+
private int id;
1012
private String name;
1113
private String description;
12-
private float price;
14+
private BigDecimal price;
15+
16+
public int getId()
17+
{
18+
return id;
19+
}
20+
public void setId(int id)
21+
{
22+
this.id = id;
23+
}
1324
public String getName()
1425
{
1526
return name;
@@ -26,13 +37,13 @@ public void setDescription(String description)
2637
{
2738
this.description = description;
2839
}
29-
public float getPrice()
40+
public BigDecimal getPrice()
3041
{
3142
return price;
3243
}
33-
public void setPrice(float price)
44+
public void setPrice(BigDecimal bigDecimal)
3445
{
35-
this.price = price;
46+
this.price = bigDecimal;
3647
}
3748

3849
}

spring-mvc/java-spring/src/main/webapp/WEB-INF/spring/springmvc-config.xml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
32
<beans xmlns="http://www.springframework.org/schema/beans"
4-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5-
xsi:schemaLocation="http://www.springframework.org/schema/beans
6-
http://www.springframework.org/schema/beans/spring-beans.xsd">
7-
8-
<bean name="/input-product" class="com.spring.controller.InputProductController" />
9-
<bean name="/save-product" class="com.spring.controller.SaveProductController" />
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:p="http://www.springframework.org/schema/p"
5+
xmlns:mvc="http://www.springframework.org/schema/mvc"
6+
xmlns:context="http://www.springframework.org/schema/context"
7+
xsi:schemaLocation="
8+
http://www.springframework.org/schema/beans
9+
http://www.springframework.org/schema/beans/spring-beans.xsd
10+
http://www.springframework.org/schema/mvc
11+
http://www.springframework.org/schema/mvc/spring-mvc.xsd
12+
http://www.springframework.org/schema/context
13+
http://www.springframework.org/schema/context/spring-context.xsd">
14+
15+
<context:component-scan base-package="com.spring.controller"/>
16+
<mvc:annotation-driven/>
17+
<mvc:resources mapping="/css/**" location="/css/"/>
18+
<mvc:resources mapping="/*.html" location="/"/>
1019

1120
<bean id="viewResolver"
1221
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
Binary file not shown.

spring-mvc/java-spring/target/m2e-wtp/web-resources/META-INF/maven/com.spring/java-spring/pom.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Generated by Maven Integration for Eclipse
2-
#Sun Feb 05 16:16:15 CST 2017
3-
version=1.0-SNAPSHOT
2+
#Sun Feb 05 19:41:00 CST 2017
3+
version=1.0.0
44
groupId=com.spring
55
m2e.projectName=java-spring
66
m2e.projectLocation=G\:\\M_example\\java\\java-spring

spring-mvc/java-spring/target/m2e-wtp/web-resources/META-INF/maven/com.spring/java-spring/pom.xml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.spring</groupId>
55
<artifactId>java-spring</artifactId>
66
<packaging>war</packaging>
7-
<version>1.0-SNAPSHOT</version>
7+
<version>1.0.0</version>
88
<name>java-spring Maven Webapp</name>
99
<url>http://maven.apache.org</url>
1010

@@ -17,7 +17,7 @@
1717
<scope>test</scope>
1818
</dependency>
1919

20-
<!--3.Servlet web相关依赖 -->
20+
<!--1.Servlet web相关依赖 -->
2121
<dependency>
2222
<groupId>taglibs</groupId>
2323
<artifactId>standard</artifactId>
@@ -28,18 +28,13 @@
2828
<artifactId>jstl</artifactId>
2929
<version>1.2</version>
3030
</dependency>
31-
<dependency>
32-
<groupId>com.fasterxml.jackson.core</groupId>
33-
<artifactId>jackson-databind</artifactId>
34-
<version>2.5.4</version>
35-
</dependency>
3631
<dependency>
3732
<groupId>javax.servlet</groupId>
3833
<artifactId>javax.servlet-api</artifactId>
3934
<version>3.1.0</version>
4035
</dependency>
4136

42-
<!--4:spring依赖 -->
37+
<!--2:spring依赖 -->
4338
<!--1)spring核心依赖 -->
4439
<dependency>
4540
<groupId>org.springframework</groupId>
@@ -84,8 +79,8 @@
8479
<artifactId>spring-test</artifactId>
8580
<version>4.1.7.RELEASE</version>
8681
</dependency>
87-
8882
</dependencies>
83+
8984
<build>
9085
<finalName>java-spring</finalName>
9186
</build>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy