springMVC + maven helloworld程序
来源:互联网 发布:沈阳系统i5编程实例 编辑:程序博客网 时间:2024/05/20 14:18
springMVC + maven helloworld程序(参考书籍《springMVC学习指南》)
1、创建一个空的maven项目。mvn archetype:generate -DarchetypeCatalog=internal
2、引入所需的jar包。
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.0.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.0.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.0.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.0.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.0.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.0.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jms --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>4.0.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc-portlet --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc-portlet</artifactId> <version>4.0.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-messaging --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-messaging</artifactId> <version>4.0.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-instrument --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-instrument</artifactId> <version>4.0.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-framework-bom --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-framework-bom</artifactId> <version>4.0.5.RELEASE</version> <type>pom</type> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.0.5.RELEASE</version> </dependency>程序文件分布:
各个文件中的内容:
InputPruductController.java
package com.xueyoucto.xueyou.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 request, HttpServletResponse response) throws Exception {// TODO Auto-generated method stublogger.info("InputProductController called!");return new ModelAndView("ProductForm");}}
SaveProductController
package com.xueyoucto.xueyou.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 com.xueyoucto.xueyou.domain.Product;import com.xueyoucto.xueyou.form.ProductForm;public class SaveProductController implements Controller{private static final Log logger = LogFactory.getLog(SaveProductController.class);public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {// TODO Auto-generated method stublogger.info("SaveProductController called");ProductForm productForm = new ProductForm();productForm.setName(request.getParameter("name"));productForm.setDescription(request.getParameter("description"));productForm.setPrice(request.getParameter("price"));Product product = new Product();product.setName(productForm.getName());product.setDescription(productForm.getDescription());try {product.setPrice(Float.parseFloat(productForm.getPrice()));} catch (NumberFormatException e) {// TODO Auto-generated catch blocke.printStackTrace();}request.setAttribute("product", product);return new ModelAndView("ProductDetails");}}
Product.java
package com.xueyoucto.xueyou.domain;import java.io.Serializable;public class Product implements Serializable {/** * */private static final long serialVersionUID = -7123707721242216314L;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;}}
package com.xueyoucto.xueyou.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;}}
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><div><p><h5>detail</h5>product name:${product.name}<br/>Description:${product.description}<br/>Price:$${product.price}</p></div></body></html>
ProductForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><form action="product_save.action" method="get"><p><label>product name:</label> <input type="text" id="name" name="name" /></p><p><label>product price:</label> <input type="price" id="name" name="price" /></p><p><label>product description:</label> <input type="text" id="description" name="description" /></p><p><input id="reset" type="reset"/><input id="submit" type="submit"/></p></form></body></html>
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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean name="/product_input.action"class="com.xueyoucto.xueyou.controller.InputProductController"></bean><bean name="/product_save.action" class="com.xueyoucto.xueyou.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><bean name="/testhtml.action"class="com.xueyoucto.xueyou.controller.TestHtml"></bean></beans>
web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app> <display-name>Archetype Created Web Application</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>/</url-pattern> </servlet-mapping></web-app>
点击“提交”
0 0
- springMVC + maven helloworld程序
- springMVC 入门程序 helloworld!
- SpringMVC创建HelloWorld程序
- springMVC入门程序helloworld
- Maven+SpringMVC整合之helloworld
- Maven+SpringMVC整合之helloworld
- Maven+SpringMVC整合之helloworld
- Maven+SpringMVC整合之helloworld
- Maven+SpringMVC整合之helloworld
- SpringMVC创建一个HelloWorld程序
- idea:用maven、Spring+SpringMVC写HelloWorld
- SpringMVC---HelloWorld程序的创建步骤
- 第一个配置SpringMVC,HelloWorld程序
- SpringMVC基础入门,创建一个HelloWorld程序
- SpringMVC基础入门--创建一个HelloWorld程序
- SpringMVC基础入门,创建一个HelloWorld程序
- SpringMVC基础入门,创建一个HelloWorld程序
- aspectJ——maven HelloWorld程序
- 198-213. House RobberI&II
- C++的拷贝构造函数
- js中的this关键字详解
- Day6、双重循环、while..do循环、随机数、缓冲区、一位数组、判断质数、算出最大公约数
- 各种编码之间的区别:ASCII、Unicode、UTF-8
- springMVC + maven helloworld程序
- 前端开发面试题 (题目列表页)
- Java 父类的构造函数执行要早于子类的实例变量初始化
- 【Leetcode】之Validate Binary Search Tree
- Java项目如何打包Jar并且随处运行?
- HttpSession的正确理解
- 深究递归和迭代的区别、联系、优缺点及实例对比
- Win2003下安装MSN
- 虚拟主机和云服务器的区别