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">
- <!-- spring配置 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:config/spring-*.xml</param-value>
- </context-param>
- <!-- 配置spring启动listener入口 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- 配置springMVC启动DispatcherServlete入口 -->
- <servlet>
- <servlet-name>springMVC</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:config/spring-annotation.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>springMVC</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <!-- 乱码过滤器配置 -->
- <filter>
- <filter-name>CharacterEncodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>utf-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CharacterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- 配置Session -->
- <filter>
- <filter-name>openSession</filter-name>
- <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>openSession</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- </web-app>
spring-annotation.xml
spring-hibernate.xml
spring-core.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
- <!-- 导入spring配置 -->
- <import
- resource="classpath*:com/xdy/spring/springAnnotation-import.xml" />
- </beans>
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
- <context:component-scan base-package="com.xdy" />
- <mvc:annotation-driven />
- <!-- 静态资源访问 -->
- <mvc:resources location="/img/" mapping="/img/**" />
- <mvc:resources location="/js/" mapping="/js/**" />
- <!-- 配置解析器 -->
- <bean id="viewResolver"
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/" />
- <property name="suffix" value=".jsp" />
- </bean>
- <!-- 支持上传文件 -->
- <bean id="multipartResolver"
- class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <property name="defaultEncoding" value="utf-8"></property>
- <property name="maxUploadSize" value="10485760000"></property>
- <property name="maxInMemorySize" value="40960"></property>
- </bean>
- </beans>
User.java
- package com.xdy.entity;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.Table;
- import org.hibernate.annotations.GenericGenerator;
- @Entity
- @Table(name="t_user")
- public class User {
- @Id
- @GeneratedValue(generator="system-uuid")
- @GenericGenerator(name = "system-uuid",strategy="uuid")
- @Column(length=32)
- private String id;
- @Column(length=32)
- private String name;
- @Column(length=32)
- private Integer age;
- public String getId() {
- return id;
- }
- public void setId(String 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;
- }
- }
- package com.xdy.dao;
- import java.util.List;
- import com.xdy.entity.User;
- public interface UserDao {
- public void addUser(User user);
- public boolean deleteUser(String id);
- public boolean modifyUser(User user);
- public List<User> getAll();
- public User getUser(String id);
- }
UserService.java
- package com.xdy.service;
- import java.util.List;
- import com.xdy.entity.User;
- public interface UserService {
- public void addUser(User user);
- public List<User> getAll();
- public boolean deleteUser(String id);
- public User getUser(String id);
- public boolean modifyUser(User user);
- }
- package com.xdy.service.impl;
- import java.util.List;
- import com.xdy.dao.UserDao;
- import com.xdy.entity.User;
- import com.xdy.service.UserService;
- public class UserServiceImpl implements UserService {
- private UserDao userDao;
- public void setUserDao(UserDao userDao) {
- this.userDao = userDao;
- }
- @Override
- public void addUser(User user) {
- userDao.addUser(user);
- }
- @Override
- public List<User> getAll() {
- return userDao.getAll();
- }
- @Override
- public boolean deleteUser(String id) {
- return userDao.deleteUser(id);
- }
- @Override
- public User getUser(String id) {
- return userDao.getUser(id);
- }
- @Override
- public boolean modifyUser(User user) {
- return userDao.modifyUser(user);
- }
- }
4、前端页面
addUser.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>添加用户</title>
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- </head>
- <body>
- <form action="/springMvc11/user/addUser" method="post">
- 用户名:<input type="text" name="name"><br/>
- 年龄:<input type="text" name="age"><br/>
- <input type="submit" value="添加用户">
- </form>
- </body>
- </html>
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>修改用户</title>
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- </head>
- <body>
- <form action="/springMvc11/user/modifyUser" method="post">
- <input type="hidden" name="id" value="${user.id }">
- 用户名:<input type="text" name="name" value="${user.name }"><br/>
- 年龄:<input type="text" name="age" value="${user.age }"><br/>
- <input type="submit" value="修改">
- </form>
- </body>
- </html>
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>用户列表</title>
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <script type="text/javascript" src="../js/jquery-1.4.3.js"></script>
- <script type="text/javascript">
- function deleteUser(id){
- $.get("/springMvc11/user/deleteUser?id=" + id,function(data){
- if("success" == data.result){
- alert("删除成功!");
- window.location.reload();
- }else{
- alert("删除失败!")
- }
- });
- }
- </script>
- </head>
- <body>
- <table border="1">
- <tbody>
- <tr>
- <th>名字</th>
- <th>年龄</th>
- <th>操作</th>
- </tr>
- <c:forEach items="${users}" var="u">
- <tr>
- <td>${u.name }</td>
- <td>${u.age }</td>
- <td>
- <a href="/springMvc11/user/getUser?id=${u.id }">更新</a>
- <a href="javascript:deleteUser('${u.id }');">删除</a>
- </td>
- </tr>
- </c:forEach>
- </tbody>
- </table>
- </body>
- </html>