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. jdbc连接rac的oracle数据库

    jdbc连接rac的oracle数据库需要配置所有racIP,如下: DB1 =(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(H ...

  2. Json解析教程(四.FastJson 的使用)

    简介 Fastjson是一个Java语言编写的高性能功能完善的JSON库. 高性能 fastjson采用独创的算法,将parse的速度提升到极致,超过所有json库,包括曾经号称最快的jackson. ...

  3. Coursera课程《Python数据结构》中课程目录

    Python Data Structures Python Data Structures is the second course in the specialization Python for ...

  4. 【BZOJ】【2752】【HAOI2012】高速公路(Road)

    数学期望/线段树 然而又是一道road= =上一道是2750…… 下次不要一看期望题就弃疗么…… 期望题≠不可做题……!! 其实在这题中,期望就是(所有情况下 权值之和)/(总方案数) 因为是等概率抽 ...

  5. QT学习:c++解析html相关

    原来我做爬虫的时候,对页面进行解析的时候总是用很简单粗暴的方法,直接找规律.后来在网上看到了gumbo,尝试了一下,发现确实很好用,所以向大家推荐一下. 以下转自:http://blog.csdn.n ...

  6. go语言基础之切片和底层数组关系

    1.切片和底层数组关系 示例: package main //必须有个main包 import "fmt" func main() { a := []int{0, 1, 2, 3, ...

  7. 微博推荐算法学习(Weibo Recommend Algolrithm)

    原文:http://hijiangtao.github.io/2014/10/06/WeiboRecommendAlgorithm/ 基础及关联算法 作用:为微博推荐挖掘必要的基础资源.解决推荐时的通 ...

  8. [Javascript]Clouse Cove, 2 ,Modifying Bound Values After Closure

    function buildCoveTicketMarker(transport){ var passengerNumber = 0; return function(name){ passenger ...

  9. Python标准库:内置函数abs(x)

    返回数字的绝对值. 參数能够是整数或浮点数.假设參数是复数,则返回复数的模. 因此abs()函数的注意点就是复数的不一样计算方式. 样例: #正整数 print('abs(1):', abs(1)) ...

  10. uni - 条件渲染

    vue官方文档和uni官方同步:https://cn.vuejs.org/v2/guide/conditional.html 1.多次切换建议使用v-show(始终保存在BOM) 2.因为if是惰性判 ...