第一步:下載spring下載鏈接:http://repo.springsource.org/libs-release-local/org/springframework/spring/
下載後的文檔裏面有相關api和講解
第二部:在eclipse裏面部署springmvc
首先目錄結構如下:
首先導入springmvc需要的jar包一共20個,外加日誌文件common-log.jar這是必要文件,另外列子需要jstl的兩個jar文件
lib文件夾下面如下
第三步:在web.xml配置springmvc
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.fruit</url-pattern> <!-- 什麼樣的請求會被springmvc處理,不能是/*.fruit這是不對的 -->
</servlet-mapping>
當然還有其他的配置方法,然而爸爸就覺得這一種舒服,這種配置方法會自動加載WEB-INF下的spring的springmvc-servlet.xml配置文件如果需要在其他路徑下配置springmvc-servlet的話方式如下:
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/某某路徑(如:WEB-INF/classes)/自己指定的配置文件名稱.xml(如:***.xml)</param-value>
<!-- classpath:與classpath*:的區別是前者僅指我們所給出的當前目錄,而後者這包括當前目錄的子目錄 --> </init-param>
<load-on-startup>1</load-on-startup>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.fruit</url-pattern>
</servlet-mapping>
第四步:配置springmvc-servlet.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="app03a"/><!-- 註解支持,添加要掃描的包 -->
<bean name="/product_input.fruit" class="app03a.controller.InputProductController"></bean> <!-- 自定義控制器 -->
<bean name="/product_save.fruit" class="app03a.controller.SaveProductController"></bean> <!-- 自定義控制器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean> <!-- 視圖解析器 -->
<!-- ... -->
</beans>
配置springmvc作者走過的坑,要注意添加掃描的包,你自己在那些類裏面添加了spring的註釋就要添加掃描其所在的包,視圖解析器就是來去掉一些前綴和後綴的。
這樣就基本上可以運行了,另外提醒新手們市面上的書大都不適合新版的spring,所以只能拿來參考,具體是要以spring官方說明爲準。spring說明在下載的壓縮包裏面,spring4.3.5\spring-framework-4.3.5.RELEASE\docs\spring-framework-reference\htmlsingle\index.html。要多多參考這個,裏面都能找到!
樓主再次放我的項目代碼
package app03a.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 app03a.controller;
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 app03a.domain.Product;
import app03a.form.ProductForm;
@Controller
@RequestMapping(path="/product")
public class ProductController {
private static final Log logger = LogFactory.getLog(ProductController.class);
@RequestMapping("/input")
public String inputProduct() {
logger.info("inputProduct called");
return "ProductForm";
}
@RequestMapping("/save")
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(Float.parseFloat(
productForm.getPrice()));
} catch (NumberFormatException e) {
}
// add product
model.addAttribute("product", product);
return "ProductDetails";
}
}
package app03a.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 app03a.domain.Product;
import app03a.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 productForm = new ProductForm();
// populate action properties
productForm.setName(request.getParameter("name"));
productForm.setDescription(request.getParameter("description"));
productForm.setPrice(request.getParameter("price"));
// create model
Product product = new Product();
product.setName(productForm.getName());
product.setDescription(productForm.getDescription());
try {
product.setPrice(Float.parseFloat(productForm.getPrice()));
} catch (NumberFormatException e) {
}
// insert code to save Product
return new ModelAndView("ProductDetails", "product",
product);
}
}
package app03a.domain;
import java.io.Serializable;
public class Product implements Serializable {
private static final long serialVersionUID = 748392348L;
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;
}
}
ProductDetails.jsp
package app03a.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;
}
}
ProductForm.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>
<!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.fruit" method="post">
<fieldset>
<legend>Add a product</legend>
<p>
<label for="name">Product Name: </label>
<input type="text" id="name" name="name"
tabindex="1">
</p>
<p>
<label for="description">Description: </label>
<input type="text" id="description"
name="description" tabindex="2">
</p>
<p>
<label for="price">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="Add Product">
</p>
</fieldset>
</form>
</div>
</body>
</html>