Skip to content

Commit 0f8d152

Browse files
committed
spring-mvc chapter3
1 parent a0f7946 commit 0f8d152

28 files changed

+594
-0
lines changed

spring-mvc/java-spring/.classpath

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
15+
<attributes>
16+
<attribute name="optional" value="true"/>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
21+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
22+
<attributes>
23+
<attribute name="maven.pomderived" value="true"/>
24+
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
25+
</attributes>
26+
</classpathentry>
27+
<classpathentry kind="output" path="target/classes"/>
28+
</classpath>

spring-mvc/java-spring/.project

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>java-spring</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.jdt.core.javabuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.wst.common.project.facet.core.builder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
<buildCommand>
24+
<name>org.eclipse.m2e.core.maven2Builder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
<buildCommand>
29+
<name>org.eclipse.wst.validation.validationbuilder</name>
30+
<arguments>
31+
</arguments>
32+
</buildCommand>
33+
</buildSpec>
34+
<natures>
35+
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
36+
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
37+
<nature>org.eclipse.jdt.core.javanature</nature>
38+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
39+
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
40+
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
41+
</natures>
42+
</projectDescription>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry excluding="**/bower_components/*|**/node_modules/*|**/*.min.js" kind="src" path="src/main/webapp"/>
4+
<classpathentry kind="src" path="target/m2e-wtp/web-resources"/>
5+
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
6+
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
7+
<attributes>
8+
<attribute name="hide" value="true"/>
9+
</attributes>
10+
</classpathentry>
11+
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
12+
<classpathentry kind="output" path=""/>
13+
</classpath>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
4+
org.eclipse.jdt.core.compiler.compliance=1.5
5+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
6+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
7+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
8+
org.eclipse.jdt.core.compiler.source=1.5
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
2+
<wb-module deploy-name="java-spring">
3+
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
4+
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
5+
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
6+
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
7+
<property name="context-root" value="java-spring"/>
8+
<property name="java-output-path" value="/java-spring/target/classes"/>
9+
</wb-module>
10+
</project-modules>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<faceted-project>
3+
<fixed facet="wst.jsdt.web"/>
4+
<installed facet="java" version="1.5"/>
5+
<installed facet="jst.web" version="2.3"/>
6+
<installed facet="wst.jsdt.web" version="1.0"/>
7+
</faceted-project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.eclipse.wst.jsdt.launching.baseBrowserLibrary
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Window
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
disabled=06target
2+
eclipse.preferences.version=1

spring-mvc/java-spring/pom.xml

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.spring</groupId>
5+
<artifactId>java-spring</artifactId>
6+
<packaging>war</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>java-spring Maven Webapp</name>
9+
<url>http://maven.apache.org</url>
10+
11+
12+
<dependencies>
13+
<dependency>
14+
<groupId>junit</groupId>
15+
<artifactId>junit</artifactId>
16+
<version>4.11</version>
17+
<scope>test</scope>
18+
</dependency>
19+
20+
<!--3.Servlet web相关依赖 -->
21+
<dependency>
22+
<groupId>taglibs</groupId>
23+
<artifactId>standard</artifactId>
24+
<version>1.1.2</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>jstl</groupId>
28+
<artifactId>jstl</artifactId>
29+
<version>1.2</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>com.fasterxml.jackson.core</groupId>
33+
<artifactId>jackson-databind</artifactId>
34+
<version>2.5.4</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>javax.servlet</groupId>
38+
<artifactId>javax.servlet-api</artifactId>
39+
<version>3.1.0</version>
40+
</dependency>
41+
42+
<!--4:spring依赖 -->
43+
<!--1)spring核心依赖 -->
44+
<dependency>
45+
<groupId>org.springframework</groupId>
46+
<artifactId>spring-core</artifactId>
47+
<version>4.1.7.RELEASE</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.springframework</groupId>
51+
<artifactId>spring-beans</artifactId>
52+
<version>4.1.7.RELEASE</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.springframework</groupId>
56+
<artifactId>spring-context</artifactId>
57+
<version>4.1.7.RELEASE</version>
58+
</dependency>
59+
<!--2)spring dao层依赖 -->
60+
<dependency>
61+
<groupId>org.springframework</groupId>
62+
<artifactId>spring-jdbc</artifactId>
63+
<version>4.1.7.RELEASE</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.springframework</groupId>
67+
<artifactId>spring-tx</artifactId>
68+
<version>4.1.7.RELEASE</version>
69+
</dependency>
70+
<!--3)springweb相关依赖 -->
71+
<dependency>
72+
<groupId>org.springframework</groupId>
73+
<artifactId>spring-web</artifactId>
74+
<version>4.1.7.RELEASE</version>
75+
</dependency>
76+
<dependency>
77+
<groupId>org.springframework</groupId>
78+
<artifactId>spring-webmvc</artifactId>
79+
<version>4.1.7.RELEASE</version>
80+
</dependency>
81+
<!--4)spring test相关依赖 -->
82+
<dependency>
83+
<groupId>org.springframework</groupId>
84+
<artifactId>spring-test</artifactId>
85+
<version>4.1.7.RELEASE</version>
86+
</dependency>
87+
88+
</dependencies>
89+
<build>
90+
<finalName>java-spring</finalName>
91+
</build>
92+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.spring.controller;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
import javax.servlet.http.HttpServletResponse;
5+
6+
import org.apache.commons.logging.Log;
7+
import org.apache.commons.logging.LogFactory;
8+
import org.springframework.web.servlet.ModelAndView;
9+
import org.springframework.web.servlet.mvc.Controller;
10+
11+
public class InputProductController implements Controller
12+
{
13+
private static final Log logger = LogFactory.getLog(InputProductController.class);
14+
15+
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception
16+
{
17+
logger.info("InputProductController called");
18+
return new ModelAndView("ProductForm");
19+
}
20+
21+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.spring.controller;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
import javax.servlet.http.HttpServletResponse;
5+
6+
import org.apache.commons.logging.Log;
7+
import org.apache.commons.logging.LogFactory;
8+
import org.springframework.web.servlet.ModelAndView;
9+
import org.springframework.web.servlet.mvc.Controller;
10+
11+
import com.spring.domain.Product;
12+
import com.spring.from.ProductForm;
13+
14+
public class SaveProductController implements Controller
15+
{
16+
private static final Log logger = LogFactory.getLog(SaveProductController.class);
17+
18+
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
19+
{
20+
logger.info("SaveProductController called");
21+
ProductForm productForm = new ProductForm();
22+
// populate action properties
23+
productForm.setName(request.getParameter("name"));
24+
productForm.setDescription(request.getParameter("description"));
25+
productForm.setPrice(request.getParameter("price"));
26+
27+
// create model
28+
Product product = new Product();
29+
product.setName(productForm.getName());
30+
product.setDescription(productForm.getDescription());
31+
try
32+
{
33+
product.setPrice(Float.parseFloat(productForm.getPrice()));
34+
}
35+
catch (NumberFormatException e)
36+
{
37+
throw new NumberFormatException("Failed to set price.");
38+
}
39+
40+
// insert code to save Product
41+
42+
return new ModelAndView("ProductDetails", "product", product);
43+
}
44+
45+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.spring.domain;
2+
3+
import java.io.Serializable;
4+
5+
public class Product implements Serializable
6+
{
7+
8+
private static final long serialVersionUID = 2519593427357947906L;
9+
10+
private String name;
11+
private String description;
12+
private float price;
13+
public String getName()
14+
{
15+
return name;
16+
}
17+
public void setName(String name)
18+
{
19+
this.name = name;
20+
}
21+
public String getDescription()
22+
{
23+
return description;
24+
}
25+
public void setDescription(String description)
26+
{
27+
this.description = description;
28+
}
29+
public float getPrice()
30+
{
31+
return price;
32+
}
33+
public void setPrice(float price)
34+
{
35+
this.price = price;
36+
}
37+
38+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.spring.from;
2+
3+
public class ProductForm
4+
{
5+
private String name;
6+
private String description;
7+
private String price;
8+
public String getName()
9+
{
10+
return name;
11+
}
12+
public void setName(String name)
13+
{
14+
this.name = name;
15+
}
16+
public String getDescription()
17+
{
18+
return description;
19+
}
20+
public void setDescription(String description)
21+
{
22+
this.description = description;
23+
}
24+
public String getPrice()
25+
{
26+
return price;
27+
}
28+
public void setPrice(String price)
29+
{
30+
this.price = price;
31+
}
32+
33+
34+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7+
<title>Save product</title>
8+
<style type="text/css">
9+
@import url(/css/main.css);
10+
</style>
11+
</head>
12+
<body>
13+
<div id="global">
14+
<h4>The product has been saved.</h4>
15+
<p>
16+
<h5>Details:</h5>
17+
Product Name: ${product.name}<br/> Description:
18+
${product.description}<br/> Price: $${product.price}
19+
</p>
20+
</div>
21+
</body>
22+
</html>

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