springmvc+spring框架

jar包

springmvc+spring框架
 1 com.springsource.javax.validation-1.0.0.GA.jar

 2 com.springsource.org.aopalliance-1.0.0.jar

 3 com.springsource.org.apache.commons.logging-1.1.1.jar

 4 commons-fileupload-1.2.2.jar

 5 commons-io-2.0.1.jar

 6 hibernate-validator-4.1.0.Final.jar

 7 jstl.jar

 8 log4j.jar

 9 slf4j-api-1.5.8.jar

10 slf4j-log4j12.jar

11 spring-aop-3.2.2.jar

12 spring-aspects-3.2.2.jar

13 spring-beans-3.2.2.jar

14 spring-context-3.2.2.jar

15 spring-context-support-3.2.2.jar

16 spring-core-3.2.2.jar

17 spring-expression-3.2.2.jar

18 spring-tx-3.2.2.jar

19 spring-web-3.2.2.jar

20 spring-webmvc-3.2.2.jar

21 standard.jar
View Code

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>

 2 <web-app version="2.5" 

 3     xmlns="http://java.sun.com/xml/ns/javaee" 

 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 7     

 8     <context-param>

 9         <param-name>contextConfigLocation</param-name>

10         <param-value>classpath:beans.xml</param-value>

11     </context-param>

12     <listener>

13         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

14     </listener>

15     

16     <servlet>

17         <servlet-name>action</servlet-name>

18         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

19         <init-param>

20             <param-name>contextConfigLocation</param-name>

21             <param-value>classpath:spring-mvc.xml</param-value>

22         </init-param>

23         <load-on-startup>1</load-on-startup>

24     </servlet>

25     

26     <servlet-mapping>

27         <servlet-name>action</servlet-name>

28         <url-pattern>/</url-pattern>

29     </servlet-mapping>

30     

31   <welcome-file-list>

32     <welcome-file>index.jsp</welcome-file>

33   </welcome-file-list>

34 </web-app>

beans.xml

 1 <?xml version="1.0" encoding="UTF-8"?>

 2 <beans xmlns="http://www.springframework.org/schema/beans"

 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

 4     xmlns:mvc="http://www.springframework.org/schema/mvc"

 5     xmlns:context="http://www.springframework.org/schema/context"

 6     xsi:schemaLocation="http://www.springframework.org/schema/beans 

 7             http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 

 8             http://www.springframework.org/schema/mvc 

 9             http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 

10             http://www.springframework.org/schema/context 

11             http://www.springframework.org/schema/context/spring-context-3.2.xsd ">

12 

13     <!-- 自动扫描 -->

14     <context:component-scan base-package="cn.itcast.springmvc.service"/>

15     

16 </beans>

spring-mvc.xml

 1 <?xml version="1.0" encoding="UTF-8"?>

 2 <beans xmlns="http://www.springframework.org/schema/beans"

 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

 4     xmlns:mvc="http://www.springframework.org/schema/mvc"

 5     xmlns:context="http://www.springframework.org/schema/context"

 6     xsi:schemaLocation="http://www.springframework.org/schema/beans 

 7             http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 

 8             http://www.springframework.org/schema/mvc 

 9             http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 

10             http://www.springframework.org/schema/context 

11             http://www.springframework.org/schema/context/spring-context-3.2.xsd ">

12 

13     <!-- 自动扫描 -->

14     <context:component-scan base-package="cn.itcast.springmvc.web.controller"/>

15     

16     <!-- 注解驱动 -->

17     <mvc:annotation-driven/>

18     

19     <!-- 资源管理 -->

20     <mvc:resources location="/resources/" mapping="/resources/**"/>

21     <mvc:resources location="/upload/" mapping="/upload/**"/>

22     

23     <!-- 上传文件解析器 -->

24     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

25         <property name="maxUploadSize" value="10485670"/> <!-- 10M -->

26     </bean>

27     

28     <!-- 内部资源视图解析器  prefix + logicName + suffix /WEB-INF/jsps/ + index + .jsp-->

29     <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

30         <!-- 前缀 -->

31         <property name="prefix" value="/WEB-INF/jsps/"/>

32         <!-- 后缀 -->

33         <property name="suffix" value=".jsp"/>

34     </bean>

35     

36 </beans>

HomeController.java

 1 package cn.itcast.springmvc.web.controller;

 2 

 3 import org.springframework.stereotype.Controller;

 4 import org.springframework.web.bind.annotation.RequestMapping;

 5 

 6 @Controller

 7 public class HomeController {

 8 

 9     @RequestMapping(value={"/","/home.action"})

10     public String goHome(){

11         System.out.println("Brave New World!");

12         

13         return "index";

14     }

15 }

PersonController.java

  1 package cn.itcast.springmvc.web.controller;

  2 

  3 import java.io.File;

  4 import java.io.IOException;

  5 import java.util.List;

  6 import java.util.Map;

  7 

  8 import javax.annotation.Resource;

  9 import javax.servlet.ServletContext;

 10 import javax.servlet.http.HttpServletRequest;

 11 import javax.validation.Valid;

 12 

 13 import org.apache.commons.io.FileUtils;

 14 import org.springframework.stereotype.Controller;

 15 import org.springframework.ui.Model;

 16 import org.springframework.validation.BindingResult;

 17 import org.springframework.web.bind.annotation.RequestMapping;

 18 import org.springframework.web.bind.annotation.RequestMethod;

 19 import org.springframework.web.bind.annotation.RequestParam;

 20 import org.springframework.web.multipart.MultipartFile;

 21 

 22 import cn.itcast.springmvc.domain.Person;

 23 import cn.itcast.springmvc.service.PersonService;

 24 

 25 @Controller

 26 @RequestMapping("/person")

 27 public class PersonController {

 28 

 29     @Resource

 30     private PersonService ps;

 31     

 32     

 33     //列表

 34     @RequestMapping("/listAll")

 35     public String listAll(Map<String,Object> model){

 36         List<Person> personList = ps.listAll();

 37         model.put("personList", personList);

 38         

 39         System.out.println(" listall hello");

 40         

 41         return "person/jPersonList";

 42     }

 43     

 44     //列表

 45     @RequestMapping("/listAllOther")

 46     public String listAllOther(Model model){

 47         List<Person> personList1 = ps.listAll();

 48         model.addAttribute(personList1);

 49         

 50         System.out.println(" listallother1 hello");

 51         

 52         return "person/jPersonList";

 53     }

 54     

 55     //转向新增页面

 56     @RequestMapping(value={"/tocreate"},method=RequestMethod.GET)

 57     public String tocreate(){

 58         

 59         return "person/jPersonCreate";

 60     }

 61     

 62     //转向修改页面

 63     @RequestMapping("/toupdate")

 64     public String toupdate(Integer id, Model model){

 65         Person p = ps.get(id);

 66         model.addAttribute(p);

 67         

 68         return "person/jPersonUpdate";

 69     }

 70     

 71     //保存

 72     @RequestMapping("/saveOrUpdate")

 73     public String saveOrUpdate(  HttpServletRequest request,@Valid Person p, BindingResult br, MultipartFile photo) throws IOException{

 74         //错误处理

 75         if(br.hasErrors()){

 76             return "/person/jPersonUpdate";

 77         }

 78         

 79         //上传图片

 80         ServletContext sc = request.getSession().getServletContext();

 81         String dir = sc.getRealPath("/upload/");

 82         String fileName = photo.getOriginalFilename();

 83         

 84         //防止图片覆盖

 85         Long _l = System.nanoTime();

 86         String _extName = fileName.substring(fileName.indexOf("."));

 87         fileName = _l + _extName;

 88         

 89         FileUtils.writeByteArrayToFile(new File(dir, fileName), photo.getBytes());

 90         

 91         p.setPhotoPath("/upload/"+fileName);        //指定图片保存路径

 92         

 93         ps.save(p);

 94         return "redirect:/person/listAll";

 95     }

 96     

 97     //删除

 98     @RequestMapping("/delete")

 99     public String delete(@RequestParam("delId")Integer id){

100         ps.deleteById(id);

101         return "redirect:/person/listAll";

102     }

103 }

PersonService.java

 1 package cn.itcast.springmvc.service;

 2 

 3 import java.util.ArrayList;

 4 import java.util.HashMap;

 5 import java.util.List;

 6 import java.util.Map;

 7 

 8 import org.springframework.stereotype.Service;

 9 

10 import cn.itcast.springmvc.domain.Person;

11 

12 @Service

13 public class PersonService {

14     //初始化信息

15     private static Integer id = 0;

16     private static Map<Integer,Person> map = new HashMap<Integer,Person>();

17     static{

18         Person p = null;

19         for(int i=0;i<10;i++){

20             p = new Person();

21             

22             p.setId(id);

23             p.setName("tony"+i);

24             p.setAge(20+i);

25             

26             map.put(p.getId(), p);

27             

28             id++;

29         }

30     }

31     

32     //列表

33     public List<Person> listAll(){

34         return new ArrayList(map.values());

35     }

36     

37     //获取对象

38     public Person get(Integer id){

39         return map.get(id);

40     }

41     

42     //保存

43     public void save(Person p){

44         if(p.getId()==null){

45             id++;

46             p.setId(id);

47         }

48         map.put(p.getId(), p);

49     }

50     

51     //删除

52     public void deleteById(Integer id){

53         map.remove(id);

54     }

55 

56     

57 }

Person.java

 1 package cn.itcast.springmvc.domain;

 2 

 3 import javax.validation.constraints.NotNull;

 4 import javax.validation.constraints.Size;

 5 

 6 public class Person {

 7     private Integer id;

 8     public Integer getId() {

 9         return id;

10     }

11     public void setId(Integer id) {

12         this.id = id;

13     }

14     public String getName() {

15         return name;

16     }

17     public void setName(String name) {

18         this.name = name;

19     }

20     public Integer getAge() {

21         return age;

22     }

23     public void setAge(Integer age) {

24         this.age = age;

25     }

26     @Size(min=3,max=10,message="用户名必须为3到10个字符!")

27     private String name;

28     @NotNull(message="年龄必须填写!")

29     private Integer age;

30     private String photoPath;

31     public String getPhotoPath() {

32         return photoPath;

33     }

34     public void setPhotoPath(String photoPath) {

35         this.photoPath = photoPath;

36     }

37 }

WEB-INF下的jsps文件夹下

jPersonList.jsp

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

 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

 3 <html>

 4   <head>

 5     <title>My JSP 'index.jsp' starting page</title>

 6   </head>

 7   

 8   <body>

 9 <h2>用户列表</h2>

10     

11         <div style="padding:10px;"><a href="${pageContext.request.contextPath}/person/tocreate.action">新增</a></div>

12     

13 <table border="1">

14 <tr>

15     <td>photo</td>

16     <td>id</td>

17     <td>name</td>

18     <td>age</td>

19     <td>操作</td>

20 </tr>

21 

22 <c:forEach items="${personList}" var="p">

23 <tr>

24     <td><img src="${pageContext.request.contextPath}${p.photoPath}"/></td>

25     <td>${p.id}</td>

26     <td>${p.name}</td>

27     <td>${p.age}</td>

28     <td>

29         <a href="${pageContext.request.contextPath}/person/toupdate.action?id=${p.id}">修改</a>

30         <a href="${pageContext.request.contextPath}/person/delete.action?delId=${p.id}">删除</a>

31     </td>

32 </tr>

33 </c:forEach>

34 

35 </table>

36   </body>

37 </html>

jPersonCreate.jsp

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

 2 <html>

 3   <head>

 4     <title>My JSP 'index.jsp' starting page</title>

 5   </head>

 6   

 7   <body> 

 8 <form action="${pageContext.request.contextPath}/person/saveOrUpdate.action" method="post" enctype="multipart/form-data">

 9      <h2>新增用户</h2>

10 <table>

11 <tr>

12     <td>id:</td>

13     <td><input type="text" name="id"/></td>

14 </tr>

15 <tr>

16     <td>name:</td>

17     <td><input type="text" name="name"/></td>

18 </tr>

19 <tr>

20     <td>age:</td>

21     <td><input type="text" name="age"/></td>

22 </tr>

23 <tr>

24     <td>photo:</td>

25     <td><input type="file" name="photoPathxx"/></td>

26 </tr>

27 <tr>

28     <td colspan="2">

29         <input type="submit"/>

30     </td>

31 </tr>

32 </table>

33     

34 </form>    

35   </body>

36 </html>

jPersonUpdate.jsp

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

 2 

 3 <%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf"%>

 4 <html>

 5   <head>

 6     <title>My JSP 'index.jsp' starting page</title>

 7   </head>

 8   

 9   <body>

10 <sf:form id="p" action="saveOrUpdate" 

11     method="post" 

12     enctype="multipart/form-data"

13     modelAttribute="person">

14 

15 

16 <h2>修改用户</h2>

17     

18 <table border="1">

19 <tr>

20     <td>id:</td>

21     <td><sf:input path="id"/></td>

22 </tr>

23 <tr>

24     <td>name:</td>

25     <td><sf:input path="name"/><sf:errors path="name"/></td>

26 </tr>

27 <tr>

28     <td>age:</td>

29     <td><sf:input path="age"/><sf:errors path="age"/></td>

30 </tr>

31 <tr>

32     <td>photo:</td>

33     <td><input type="file" name="photo"/></td>

34 </tr>

35 <tr>

36     <td><input type="submit"/></td>

37 </tr>

38 

39 </table>

40 

41 </sf:form>

42   </body>

43 </html>

index.jsp

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

 2 <html>

 3   <head>

 4     <title>My JSP 'index.jsp' starting page</title>

 5   </head>

 6   

 7   <body>

 8    <img src="${pageContext.request.contextPath}/resources/girl.jpg"><br/>

 9     This is my JSP page. spring MVC Brave New World!<br/>

10   </body>

11 </html>

 

你可能感兴趣的:(springMVC)