jar包

 com.springsource.javax.validation-1.0.0.GA.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
hibernate-validator-4.1.0.Final.jar
jstl.jar
log4j.jar
slf4j-api-1.5.8.jar
slf4j-log4j12.jar
spring-aop-3.2.2.jar
spring-aspects-3.2.2.jar
spring-beans-3.2.2.jar
spring-context-3.2.2.jar
spring-context-support-3.2.2.jar
spring-core-3.2.2.jar
spring-expression-3.2.2.jar
spring-tx-3.2.2.jar
spring-web-3.2.2.jar
spring-webmvc-3.2.2.jar
standard.jar

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"> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

beans.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd "> <!-- 自动扫描 -->
<context:component-scan base-package="cn.itcast.springmvc.service"/> </beans>

spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd "> <!-- 自动扫描 -->
<context:component-scan base-package="cn.itcast.springmvc.web.controller"/> <!-- 注解驱动 -->
<mvc:annotation-driven/> <!-- 资源管理 -->
<mvc:resources location="/resources/" mapping="/resources/**"/>
<mvc:resources location="/upload/" mapping="/upload/**"/> <!-- 上传文件解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485670"/> <!-- 10M -->
</bean> <!-- 内部资源视图解析器 prefix + logicName + suffix /WEB-INF/jsps/ + index + .jsp-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsps/"/>
<!-- 后缀 -->
<property name="suffix" value=".jsp"/>
</bean> </beans>

HomeController.java

 package cn.itcast.springmvc.web.controller;

 import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class HomeController { @RequestMapping(value={"/","/home.action"})
public String goHome(){
System.out.println("Brave New World!"); return "index";
}
}

PersonController.java

 package cn.itcast.springmvc.web.controller;

 import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map; import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid; import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile; import cn.itcast.springmvc.domain.Person;
import cn.itcast.springmvc.service.PersonService; @Controller
@RequestMapping("/person")
public class PersonController { @Resource
private PersonService ps; //列表
@RequestMapping("/listAll")
public String listAll(Map<String,Object> model){
List<Person> personList = ps.listAll();
model.put("personList", personList); System.out.println(" listall hello"); return "person/jPersonList";
} //列表
@RequestMapping("/listAllOther")
public String listAllOther(Model model){
List<Person> personList1 = ps.listAll();
model.addAttribute(personList1); System.out.println(" listallother1 hello"); return "person/jPersonList";
} //转向新增页面
@RequestMapping(value={"/tocreate"},method=RequestMethod.GET)
public String tocreate(){ return "person/jPersonCreate";
} //转向修改页面
@RequestMapping("/toupdate")
public String toupdate(Integer id, Model model){
Person p = ps.get(id);
model.addAttribute(p); return "person/jPersonUpdate";
} //保存
@RequestMapping("/saveOrUpdate")
public String saveOrUpdate( HttpServletRequest request,@Valid Person p, BindingResult br, MultipartFile photo) throws IOException{
//错误处理
if(br.hasErrors()){
return "/person/jPersonUpdate";
} //上传图片
ServletContext sc = request.getSession().getServletContext();
String dir = sc.getRealPath("/upload/");
String fileName = photo.getOriginalFilename(); //防止图片覆盖
Long _l = System.nanoTime();
String _extName = fileName.substring(fileName.indexOf("."));
fileName = _l + _extName; FileUtils.writeByteArrayToFile(new File(dir, fileName), photo.getBytes()); p.setPhotoPath("/upload/"+fileName); //指定图片保存路径 ps.save(p);
return "redirect:/person/listAll";
} //删除
@RequestMapping("/delete")
public String delete(@RequestParam("delId")Integer id){
ps.deleteById(id);
return "redirect:/person/listAll";
}
}

PersonService.java

 package cn.itcast.springmvc.service;

 import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.springframework.stereotype.Service; import cn.itcast.springmvc.domain.Person; @Service
public class PersonService {
//初始化信息
private static Integer id = 0;
private static Map<Integer,Person> map = new HashMap<Integer,Person>();
static{
Person p = null;
for(int i=0;i<10;i++){
p = new Person(); p.setId(id);
p.setName("tony"+i);
p.setAge(20+i); map.put(p.getId(), p); id++;
}
} //列表
public List<Person> listAll(){
return new ArrayList(map.values());
} //获取对象
public Person get(Integer id){
return map.get(id);
} //保存
public void save(Person p){
if(p.getId()==null){
id++;
p.setId(id);
}
map.put(p.getId(), p);
} //删除
public void deleteById(Integer id){
map.remove(id);
} }

Person.java

 package cn.itcast.springmvc.domain;

 import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size; public class Person {
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Size(min=3,max=10,message="用户名必须为3到10个字符!")
private String name;
@NotNull(message="年龄必须填写!")
private Integer age;
private String photoPath;
public String getPhotoPath() {
return photoPath;
}
public void setPhotoPath(String photoPath) {
this.photoPath = photoPath;
}
}

WEB-INF下的jsps文件夹下

jPersonList.jsp

 <%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head> <body>
<h2>用户列表</h2> <div style="padding:10px;"><a href="${pageContext.request.contextPath}/person/tocreate.action">新增</a></div> <table border="1">
<tr>
<td>photo</td>
<td>id</td>
<td>name</td>
<td>age</td>
<td>操作</td>
</tr> <c:forEach items="${personList}" var="p">
<tr>
<td><img src="${pageContext.request.contextPath}${p.photoPath}"/></td>
<td>${p.id}</td>
<td>${p.name}</td>
<td>${p.age}</td>
<td>
<a href="${pageContext.request.contextPath}/person/toupdate.action?id=${p.id}">修改</a>
<a href="${pageContext.request.contextPath}/person/delete.action?delId=${p.id}">删除</a>
</td>
</tr>
</c:forEach> </table>
</body>
</html>

jPersonCreate.jsp

 <%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head> <body>
<form action="${pageContext.request.contextPath}/person/saveOrUpdate.action" method="post" enctype="multipart/form-data">
<h2>新增用户</h2>
<table>
<tr>
<td>id:</td>
<td><input type="text" name="id"/></td>
</tr>
<tr>
<td>name:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>age:</td>
<td><input type="text" name="age"/></td>
</tr>
<tr>
<td>photo:</td>
<td><input type="file" name="photoPathxx"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit"/>
</td>
</tr>
</table> </form>
</body>
</html>

jPersonUpdate.jsp

 <%@ page language="java" pageEncoding="UTF-8"%>

 <%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head> <body>
<sf:form id="p" action="saveOrUpdate"
method="post"
enctype="multipart/form-data"
modelAttribute="person"> <h2>修改用户</h2> <table border="1">
<tr>
<td>id:</td>
<td><sf:input path="id"/></td>
</tr>
<tr>
<td>name:</td>
<td><sf:input path="name"/><sf:errors path="name"/></td>
</tr>
<tr>
<td>age:</td>
<td><sf:input path="age"/><sf:errors path="age"/></td>
</tr>
<tr>
<td>photo:</td>
<td><input type="file" name="photo"/></td>
</tr>
<tr>
<td><input type="submit"/></td>
</tr> </table> </sf:form>
</body>
</html>

index.jsp

 <%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head> <body>
<img src="${pageContext.request.contextPath}/resources/girl.jpg"><br/>
This is my JSP page. spring MVC Brave New World!<br/>
</body>
</html>

springmvc+spring框架的更多相关文章

  1. Spring+SpringMvc+Mybatis框架集成搭建教程

    一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼 ...

  2. SSM框架-----------SpringMVC+Spring+Mybatis框架整合详细教程

    1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One  ...

  3. 手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)

    手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版) SSM(Spring+SpringMVC+Mybatis),目前较为主流的企业级架构方案.标准的MVC设计模式, ...

  4. spring+websocket综合(springMVC+spring+MyBatis这是SSM框架和websocket集成技术)

    java-websocket该建筑是easy.儿童无用的框架可以在这里下载主线和个人教学好java-websocket计划: Apach Tomcat 8.0.3+MyEclipse+maven+JD ...

  5. 《经久不衰的Spring框架:SpringMVC 统括》

    前言:经久不衰的Spring 这几年,前端技术更新换代速度之快,每一年"最火的前端技术"排行榜都会换一番场景,本当に信じかねる.是"只闻新人笑不见旧人哭",还是 ...

  6. 《经久不衰的Spring框架:Spring+SpringMVC+MyBatis 整合》

    前言 主角即Spring.SpringMVC.MyBatis,即所谓的SSM框架,大家应该也都有所了解,概念性的东西就不写了,有万能的百度.之前没有记录SSM整合的过程,这次刚刚好基于自己的一个小项目 ...

  7. Java-----SSM(SpringMVC+Spring+mybaties)框架整合

    在进行整合之前,首先了解这个框架的作用 Mybaties: 丰富的标签库,可写动态sql,并统一的在.XML文件中编写,方便统一管理,解耦 SpringMVC: 标准的MVC思想(mode,view, ...

  8. springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来

    自己从事java开发工作也有一年多了,自己却没有亲手搭建一个完整的框架.于是今天自己动手搭建一个,过程中遇到一些问题,倒腾了大半天终于搞定了. 现在给大家分享一下过程,自己也记录下来,以后学习参考使用 ...

  9. SSM(Spring + Springmvc + Mybatis)框架面试题

    JAVA SSM框架基础面试题https://blog.csdn.net/qq_39031310/article/details/83050192 SSM(Spring + Springmvc + M ...

随机推荐

  1. Ladda 应用提交表单的时候显示loading载入中 包含不同位置,不同效果

    Ladda 应用提交表单的时候显示loading载入中 包含不同位置,不同效果 不同大小.位置,效果,进度条等 演示 XML/HTML Code <article class="exa ...

  2. [leetcode]Binary Tree Maximum Path Sum @ Python

    原题地址:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 题意: Given a binary tree, find th ...

  3. [leetcode]Populating Next Right Pointers in Each Node II @ Python

    原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题意: Follow up ...

  4. 关于Java的一些NIO框架的一点想法

    闲着有点无聊想写点东西. 问题:生活中工作中,会有人问我javaNIO框架里面 Netty Mina  xSocket Grizzly 等等哪个比较好? 在这里写一下自己的感受,也算是总结一下吧 在我 ...

  5. Java 与 JavaScript 对websocket的使用

    ebsocket,HTML5中新一代全双工通信协议.其底层仍然是http协议. 传统 HTTP 请求响应客户端服务器交互图 WebSocket 请求响应客户端服务器交互图 WebSocket 客户端支 ...

  6. [好文翻译]WEB前端性能优化的14条规则

    作为一个半前端工程师,而且只会写点HTML5和CSS3的“假”前端工程师,为了能更好地理解一下前端的花花世界,最近拜读了<高性能网站建设指南>一书,对作者提出的前端性能优化的14个规则获益 ...

  7. iphone手机微信端html5 Geolocation定位失效的问题

    使用Geolocation方法存在错误信息error.POSITION_UNAVAILABLE 其实问题不局限于微信端而是iphone升级到ios10后,对获取地理位置信息作出了限制,只有https的 ...

  8. [Backbone] Verying Views

    Below we have our AppointmentsView instance rendering and then taking the rendered HTML and insertin ...

  9. Android IO存储总结

    1 前言  android设备的存储特点:  分内存和SD卡两种存储设备,且android设备存储空间小,且系统碎片化等情况.     SD卡:老版本的android设备 不存在内置SD        ...

  10. uni-app 生命周期

    生命周期分为:页面生命周期和应用生命周期 生命周期可参考:uni-app官方API 注意平台支持,仅某个平台支持会显示,5+App是超HTML5+的App方案. 例如分享:只有小程序支持.这时我们就要 ...