一、首先,建立空的web project项目:
1.
2.
二、其次,导入先关jar包
1.将jar包导入app03\WebRoot\WEB-INF\lib目录下
三、接下来修改web.xml文件,在web中,指定我们的DispatcherServlet。(从这里进入SpringMVC的可控范围)。
1.
2.web.xml中的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<display-name></display-name>
<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>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
四、添加springmvc-servlet.xml文件
1.添加新文件至如下位置
2. 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="/product_input.action" class="app03.controller.InputProductController">
</bean>
<bean name="/product_save.action" class="app03.controller.SaveProductController">
</bean>
<!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
五 编写相关java代码类。
1.结构如下:
2. 相关代码内容
Product.java
package app03.domain;
import java.io.Serializable;
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 产品名称
*/
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;
}
@Override
public String toString() {
return "Product [name=" + name + ", description=" + description
+ ", price=" + price + "]";
}
}
InputProductController.java
package app03.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);
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
logger.info("进入 InputProductController");
//返回对应的视图地址
return new ModelAndView("ProductForm");
}
}
SaveProductController.java
package app03.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 app03.domain.Product;
public class SaveProductController implements Controller {
private static final Log logger=LogFactory.getLog(SaveProductController.class);
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
logger.info("进入 SaveProductController");
Product product =new Product();
product.setName(request.getParameter("name"));
product.setDescription(request.getParameter("description"));
product.setPrice(Float.parseFloat(request.getParameter("price")));
//数据库持久化操作
//返回对应视图路径
return new ModelAndView("ProductDetail","product",product);
}
}
六、 部署
1.配置在MyEclipse中配置自己安装的Tomcat
2.发布网站
七、 启动tomcat,并在浏览器访问
----------------------------------------------------------------------------------------------------------------------------------------------------------
到此,基础的SpringMVC项目创建成功。
----------------------------------------------------------------------------------------------------------------------------------------------------------