`

Spring MVC CRUD

 
阅读更多

1.Maven Dependency

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.fool</groupId>
	<artifactId>springmvc</artifactId>
	<name>springmvc</name>
	<packaging>war</packaging>
	<version>1.0.0-BUILD-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<org.springframework-version>3.2.2.RELEASE</org.springframework-version>
		<org.aspectj-version>1.7.2</org.aspectj-version>
		<org.slf4j-version>1.7.4</org.slf4j-version>
		<commons-dbcp-version>1.4</commons-dbcp-version>
		<hibernate-version>4.1.11.Final</hibernate-version>
		<hibernate-validator-version>4.3.1.Final</hibernate-validator-version>
		<mysql-connector-version>5.1.24</mysql-connector-version>
	</properties>

	<dependencies>
		<!-- Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework-version}</version>
			<exclusions>
				<!-- Exclude Commons Logging in favor of SLF4j -->
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>

		<!-- AspectJ -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>${org.aspectj-version}</version>
		</dependency>

		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>${commons-dbcp-version}</version>
		</dependency>

		<!-- Hibernate -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>${hibernate-version}</version>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-validator</artifactId>
			<version>${hibernate-validator-version}</version>
		</dependency>

		<!-- Logging -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${org.slf4j-version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>${org.slf4j-version}</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${org.slf4j-version}</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
			<exclusions>
				<exclusion>
					<groupId>javax.mail</groupId>
					<artifactId>mail</artifactId>
				</exclusion>
				<exclusion>
					<groupId>javax.jms</groupId>
					<artifactId>jms</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.sun.jdmk</groupId>
					<artifactId>jmxtools</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.sun.jmx</groupId>
					<artifactId>jmxri</artifactId>
				</exclusion>
			</exclusions>
			<scope>runtime</scope>
		</dependency>

		<!-- @Inject -->
		<dependency>
			<groupId>javax.inject</groupId>
			<artifactId>javax.inject</artifactId>
			<version>1</version>
		</dependency>

		<!-- Servlet -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.0.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>javax.servlet.jsp-api</artifactId>
			<version>2.2.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql-connector-version}</version>
		</dependency>

		<!-- Test -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-eclipse-plugin</artifactId>
				<version>2.9</version>
				<configuration>
					<additionalProjectnatures>
						<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
					</additionalProjectnatures>
					<additionalBuildcommands>
						<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
					</additionalBuildcommands>
					<downloadSources>true</downloadSources>
					<downloadJavadocs>true</downloadJavadocs>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.0</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<compilerArgument>-Xlint:all</compilerArgument>
					<showWarnings>true</showWarnings>
					<showDeprecation>true</showDeprecation>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>exec-maven-plugin</artifactId>
				<version>1.2.1</version>
				<configuration>
					<mainClass>org.test.int1.Main</mainClass>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

 

2.Project Directory



 

3.Spring Configuration

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>

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

	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>appServlet</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>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<filter>
		<filter-name>OpenSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>OpenSessionInViewFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

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

</web-app>

 root-context.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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<context:annotation-config />

	<context:component-scan base-package="org.fool.springmvc" />

	<context:property-placeholder location="classpath:jdbc.properties" />

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan" value="org.fool.springmvc.model" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQL5Dialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.current_session_context_class">
					org.springframework.orm.hibernate4.SpringSessionContext
				</prop>
			</props>
		</property>
	</bean>

	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<aop:config>
		<aop:pointcut id="allMethods"
			expression="execution(* org.fool.springmvc.service.*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="allMethods" />
	</aop:config>
	
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="*" propagation="REQUIRED" read-only="true" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>
</beans>

 servlet-context.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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<mvc:annotation-driven />

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

	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp" />
	</bean>

	<context:component-scan base-package="org.fool.springmvc.web" />

</beans>

 

4.src/main/resources

jdbc.properties

jdbc.databaseName=test
jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/${jdbc.databaseName}?useUnicode=true&amp;characterEncoding=UTF-8 
jdbc.username = root
jdbc.password = 123456

log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

	<!-- Appenders -->
	<appender name="console" class="org.apache.log4j.ConsoleAppender">
		<param name="Target" value="System.out" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%-5p: %c - %m%n" />
		</layout>
	</appender>
	
	<!-- Application Loggers -->
	<logger name="org.fool.springmvc">
		<level value="info" />
	</logger>
	
	<!-- 3rdparty Loggers -->
	<logger name="org.springframework.core">
		<level value="info" />
	</logger>
	
	<logger name="org.springframework.beans">
		<level value="info" />
	</logger>
	
	<logger name="org.springframework.context">
		<level value="info" />
	</logger>

	<logger name="org.springframework.web">
		<level value="info" />
	</logger>

	<!-- Root Logger -->
	<root>
		<priority value="warn" />
		<appender-ref ref="console" />
	</root>
	
</log4j:configuration>

 schema.sql

drop table if exists t_user ;

create table t_user
(
	uid integer not null auto_increment,
	username varchar(20) not null,
	password varchar(20) not null,
	nickname varchar(20) not null,
	email varchar(30) not null,
	constraint user_uid_pk primary key(uid)
);

insert into t_user(username, password, nickname, email)
values("springmvc", "springmvc", "mvc", "springmvc@mvc.org");
insert into t_user(username, password, nickname, email)
values("springmvc", "springmvc", "mvc", "springmvc@mvc.org");
insert into t_user(username, password, nickname, email)
values("springmvc", "springmvc", "mvc", "springmvc@mvc.org");
insert into t_user(username, password, nickname, email)
values("springmvc", "springmvc", "mvc", "springmvc@mvc.org");
insert into t_user(username, password, nickname, email)
values("springmvc", "springmvc", "mvc", "springmvc@mvc.org");
insert into t_user(username, password, nickname, email)
values("springmvc", "springmvc", "mvc", "springmvc@mvc.org");
insert into t_user(username, password, nickname, email)
values("springmvc", "springmvc", "mvc", "springmvc@mvc.org");
insert into t_user(username, password, nickname, email)
values("springmvc", "springmvc", "mvc", "springmvc@mvc.org");
insert into t_user(username, password, nickname, email)
values("springmvc", "springmvc", "mvc", "springmvc@mvc.org");
insert into t_user(username, password, nickname, email)
values("springmvc", "springmvc", "mvc", "springmvc@mvc.org");

commit;

 

5.src/main/java

User.java

package org.fool.springmvc.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;

@Entity
@Table(name = "t_user")
public class User
{
	private int id;
	private String username;
	private String password;
	private String nickname;
	private String email;

	@Id
	@GeneratedValue
	@Column(name = "uid")
	public int getId()
	{
		return id;
	}

	public void setId(int id)
	{
		this.id = id;
	}

	@NotEmpty(message = "username not empty")
	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	@NotEmpty(message = "password not empty")
	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	@NotEmpty(message = "nickname not empty")
	public String getNickname()
	{
		return nickname;
	}

	public void setNickname(String nickname)
	{
		this.nickname = nickname;
	}

	@NotEmpty(message = "email not empty")
	@Email(message = "email format not correct")
	public String getEmail()
	{
		return email;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}

	@Override
	public String toString()
	{
		return "User [id=" + id + ", username=" + username + ", password="
				+ password + ", nickname=" + nickname + ", email=" + email
				+ "]";
	}

}

 BaseDAO.java

package org.fool.springmvc.repository;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class BaseDAO
{
	@Resource
	private SessionFactory sessionFactory;

	protected Session getSession()
	{
		return sessionFactory.getCurrentSession();
	}
}

 UserDAO.java

package org.fool.springmvc.repository;

import java.util.List;

import org.fool.springmvc.model.User;

public interface UserDAO
{
	public void add(User user);

	public void update(User user);

	public void delete(int id);

	public User load(int id);

	public List<User> list();
}

 UserDAOImpl.java

package org.fool.springmvc.repository;

import java.util.List;

import org.fool.springmvc.model.User;
import org.springframework.stereotype.Repository;

@Repository("userDAO")
public class UserDAOImpl extends BaseDAO implements UserDAO
{

	@Override
	public void add(User user)
	{
		this.getSession().save(user);
	}

	@Override
	public void update(User user)
	{
		this.getSession().update(user);
	}

	@Override
	public void delete(int id)
	{
		this.getSession().delete(this.load(id));
	}

	@Override
	public User load(int id)
	{
		return (User) this.getSession().load(User.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> list()
	{
		return this.getSession().createQuery("from User").list();
	}

}

 UserService.java

package org.fool.springmvc.service;

import java.util.List;

import org.fool.springmvc.model.User;

public interface UserService
{
	public void add(User user);

	public void update(User user);

	public void delete(int id);

	public User load(int id);

	public List<User> list();
}

 UserServiceImpl.java

package org.fool.springmvc.service;

import java.util.List;

import javax.annotation.Resource;

import org.fool.springmvc.model.User;
import org.fool.springmvc.repository.UserDAO;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl implements UserService
{
	@Resource
	private UserDAO userDAO;

	@Override
	public void add(User user)
	{
		userDAO.add(user);
	}

	@Override
	public void update(User user)
	{
		userDAO.update(user);
	}

	@Override
	public void delete(int id)
	{
		userDAO.delete(id);
	}

	@Override
	public User load(int id)
	{
		return userDAO.load(id);
	}

	@Override
	public List<User> list()
	{
		return userDAO.list();
	}

}

 UserController.java

package org.fool.springmvc.web;

import javax.annotation.Resource;

import org.fool.springmvc.model.User;
import org.fool.springmvc.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/user")
public class UserController
{
	@Resource
	private UserService userService;

	@RequestMapping(value = "/users", method = RequestMethod.GET)
	public String list(Model model)
	{
		model.addAttribute("users", userService.list());
		return "user/list";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(@ModelAttribute("user") User user, Model model)
	{
		return "user/add";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(@Validated User user, BindingResult bindingResult)
	{
		if (bindingResult.hasErrors())
		{
			return "user/add";
		}

		userService.add(user);
		return "redirect:/user/users";
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public String show(@PathVariable int id, Model model)
	{
		model.addAttribute(userService.load(id));
		return "user/show";
	}

	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String update(@PathVariable int id, Model model)
	{
		model.addAttribute("user", userService.load(id));
		return "user/update";
	}

	@RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
	public String update(@PathVariable int id, @Validated User user,
			BindingResult bindingResult)
	{
		if (bindingResult.hasErrors())
		{
			return "user/update";
		}

		userService.update(user);

		return "redirect:/user/users";
	}

	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String delete(@PathVariable int id)
	{
		userService.delete(id);
		return "redirect:/user/users";
	}
}

 

6.src/test/java

TestUserService.java

package org.fool.springmvc.test;

import javax.annotation.Resource;

import org.fool.springmvc.model.User;
import org.fool.springmvc.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
public class TestUserService
{

	@Resource(name = "userService")
	private UserService userService;

	@Test
	public void testAdd()
	{
		User user = new User();
		user.setUsername("SpringMVC");
		user.setPassword("SpringMVC");
		user.setNickname("MVC");
		user.setEmail("spring@mvc.org");

		userService.add(user);
	}
	
}

 

7.src/main/webapp

add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!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>user add</title>
</head>
<body>
	<form:form method="post" modelAttribute="user">
		<table width="800" align="center" border="1">
			<tr>
				<td>username:</td>
				<td><form:input path="username" />
					<form:errors path="username" />
				</td>
			</tr>
			<tr>
				<td>password:</td>
				<td><form:password path="password" />
					<form:errors path="password" />
				</td>
			</tr>
			<tr>
				<td>nickname:</td>
				<td><form:input path="nickname" />
					<form:errors path="nickname" />
				</td>
			</tr>
			<tr>
				<td>email:</td>
				<td><form:input path="email" />
					<form:errors path="email" />
				</td>
			</tr>
			<tr>
				<td colspan="2" align="center">
					<input type="submit" value="add" />
				</td>
			</tr>
		</table>
	</form:form>
</body>
</html>

 list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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>user list</title>
</head>
<body>
	<table width="700" align="center" border="1">
		<c:forEach items="${users}" var="user">
			<tr>
				<td><a href="${user.id }">${user.id}</a></td>
				<td>${user.username}</td>
				<td>${user.password}</td>
				<td>${user.nickname}</td>
				<td>${user.email}</td>
				<td>
					<a href="${user.id }/update">update</a>&nbsp;
					<a href="${user.id }/delete">delete</a>
				</td>
			</tr>
		</c:forEach>
	</table>
</body>
</html>

 show.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>user info</title>
</head>
<body>
	<table width="700" align="center" border="1">
		<tr>
			<td>User ID:</td>
			<td>${user.id }</td>
		</tr>
		<tr>
			<td>Username:</td>
			<td>${user.username }</td>
		</tr>
		<tr>
			<td>Password:</td>
			<td>${user.password }</td>
		</tr>
		<tr>
			<td>Nickname:</td>
			<td>${user.nickname }</td>
		</tr>
		<tr>
			<td>Email:</td>
			<td>${user.email }</td>
		</tr>
	</table>
</body>
</html>

 update.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!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>user update</title>
</head>
<body>
	<form:form method="post" modelAttribute="user">
		<table width="800" align="center" border="1">
			<tr>
				<td>Username:</td>
				<td><form:input path="username" readonly="true" /></td>
			</tr>
			<tr>
				<td>Password:</td>
				<td><form:password path="password" />
					<form:errors path="password" />
				</td>
			</tr>
			<tr>
				<td>Nickname:</td>
				<td>
					<form:input path="nickname" />
					<form:errors path="nickname" />
				</td>
			</tr>
			<tr>
				<td>Email:</td>
				<td><form:input path="email" />
					<form:errors path="email" />
				</td>
			</tr>
			<tr>
				<td colspan="2" align="center">
					<input type="submit" value="update" />
				</td>
			</tr>
		</table>
	</form:form>
</body>
</html>

 index.jsp

<%
	response.sendRedirect("user/users");
%>

 

8.Result

 http://localhost:8080/springmvc/user/users


 

 http://localhost:8080/springmvc/user/add





 
 http://localhost:8080/springmvc/user/1/update





 

http://localhost:8080/springmvc/user/1


 

http://localhost:8080/springmvc/user/1/delete

 

select * from t_user; 


  

 

  • 大小: 20.4 KB
  • 大小: 11.7 KB
  • 大小: 85.1 KB
  • 大小: 69.1 KB
  • 大小: 73.7 KB
  • 大小: 71.4 KB
  • 大小: 86.8 KB
  • 大小: 77.4 KB
  • 大小: 78.6 KB
  • 大小: 77.3 KB
  • 大小: 88.6 KB
  • 大小: 70.9 KB
  • 大小: 88.1 KB
  • 大小: 97.5 KB
分享到:
评论

相关推荐

    一个完整的Spring MVC的CRUD操作实例

    **Spring MVC CRUD操作实例详解** Spring MVC是Spring框架的一个模块,专门用于构建Web应用程序,它提供了模型-视图-控制器(MVC)架构的支持。在本文中,我们将深入探讨如何实现一个完整的Spring MVC CRUD(创建、...

    spring MVC CRUD示例

    这个是我学习过程中的一个示例,由于对AOP的概念不是很清楚,这个例子并没有过多的涉及AOP。所以希望有人能指点我,交个朋友:QQ:657861643 本例用的开发工具是 MyEclipse6.0 tomcat6.0 ,里面的包都为MyEclipse中...

    Spring-MVC-CRUD表单提交和ajax:Spring MVC CRUD应用程序(Spring MVC 5.x,hibernate 5.x,Bootstrap 3.x,JQuery,MySql)

    Spring MVC CRUD表单提交和ajax 在这里,我提供了一个有关“ Spring MVC CRUD应用程序”的项目。 在这个项目中,我同时显示了CRUD操作的表单提交和Ajax。 让我们看看我们在这里得到了什么: 作者: Md Mamun Abdul...

    spring_mvc_crud_jdbc_template:使用JDBC模板的Spring MVC Crud

    使用JDBC模板的Spring MVC CRUD这是一个简单的SPRING MVC项目,其中我使用了Spring MVC,JDBC模板进行数据操作,而在数据库方面,我使用了MYSQL数据库。脚步- 1.创建名称为“ usersdb”的MYSQL数据库2.使用以下命令...

    Spring-Mvc-example:Spring Mvc Crud示例(Liquebase和其他)

    【Spring MVC CRUD 示例】 Spring MVC 是一个基于 Java 的轻量级 Web 开发框架,用于构建高度可维护和可测试的 MVC 应用程序。它作为 Spring 框架的一部分,提供了一种模型-视图-控制器架构,使得开发者可以轻松地...

    SpringMVC的crud实例

    SpringMVC是Spring框架的一部分,专门用于处理Web应用程序的模型-视图-控制器(MVC)架构。在本文中,我们将深入探讨如何使用SpringMVC实现CRUD(创建、读取、更新和删除)操作,这是任何数据驱动应用的基础。 首先...

    Spring MVC jar包

    Spring MVC 是一个基于Java的轻量级Web应用框架,它为开发者提供了模型-视图-控制器(MVC)架构,使开发人员能够更好地组织和分离应用程序的业务逻辑、数据处理和用户界面。Spring MVC是Spring框架的一个核心组件,...

    Spring MVC--6.RESTful SpringMVC CRUD

    在本教程中,我们将深入探讨如何使用Spring MVC构建RESTful Web服务,并实现基本的CRUD操作。Spring MVC是Spring框架的一部分,它为构建基于Java的Web应用程序提供了强大的模型-视图-控制器(MVC)架构。RESTful ...

    第四部分、Spring MVC完成CRUD.rar

    在本资源"第四部分、Spring MVC完成CRUD.rar"中,我们主要探讨的是Spring MVC框架在实际项目开发中的应用,特别是在实现CRUD(创建、读取、更新、删除)操作中的详细过程。Spring MVC是Java Web开发中常用的一个模块...

    spring mvc mybatis 整合源码,带数据库脚本,带详细注释

    Spring MVC 和 MyBatis 是两个在Java Web 开发中广泛使用的框架。Spring MVC 作为Spring框架的一部分,负责处理HTTP请求并转发到相应的业务逻辑,而MyBatis则是一个轻量级的持久层框架,用于简化数据库操作。整合这...

    spring mvc + spring + hibernate 全注解整合开发视频教程 11

    在这个视频教程中,你可能将学习到如何配置Spring的XML上下文文件以启用注解驱动的配置,如何在Spring MVC中创建注解式的控制器并处理HTTP请求,以及如何通过Hibernate的注解来设计数据模型并执行CRUD操作。...

    Ajax-Spring-MVC-CRUD-form-submit-and-ajax.zip

    Ajax-Spring-MVC-CRUD-form-submit-and-ajax.zip,spring mvc crud应用程序(springmvc、hibernate 4.x、bootstrap 3.x、jquery、mysql),ajax代表异步javascript和xml。它是多种web技术的集合,包括html、css、json...

    Spring MVC实现一次简单的CRUD示例

    Spring MVC实现CRUD示例详解 Spring MVC是一种基于Java的web应用程序框架,主要用于构建Web应用程序。CRUD(Create、Read、Update、Delete)是指在数据存储和管理系统中对数据进行创建、读取、更新和删除操作的过程...

    基于 Java ssh整合 开源博客系统 spring mvc,hibernate,spring,maven 整合开发

    例如,Spring MVC与Hibernate的结合,可以在Spring的管理下自动进行数据的CRUD操作,降低了数据库层的开发难度。同时,Spring的AOP特性可以实现全局事务管理,保证数据的一致性。Maven则简化了构建过程,使得团队...

    Spring MVC Cookbook(PACKT,2016).pdf

    《Spring MVC Cookbook》是由PACKT Publishing在2016年出版的一本专著,主要针对Spring MVC框架提供了实用的解决方案和技巧。Spring MVC是Spring框架的一部分,它为构建基于Java的Web应用程序提供了一个模型-视图-...

    Spring MVC框架简介和使用

    ### Spring MVC框架简介 Spring MVC 是一款基于 Java 的 Web 应用框架,它遵循 MVC(Model-View-Controller)设计模式。Spring MVC 提供了一种...无论是简单的 CRUD 应用还是复杂的大型系统,Spring MVC 都能够胜任。

    bbs.zip_Spring mvc bbs_hibernate bbs_spring mvc_spring mvc hiber

    《Spring MVC + Hibernate 实现BBS论坛系统详解》 在当今的互联网开发中,Spring MVC 和 Hibernate 是两个非常重要的框架,它们在企业级应用开发中起着关键作用。本项目"Spring MVC bbs"结合了Spring3和Hibernate,...

    spring mvc + spring + hibernate 全注解整合开发视频教程 12

    在本教程中,我们将深入探讨如何使用Spring MVC、Spring和Hibernate三大框架进行全注解的整合开发。这个视频教程系列的第12部分,将帮助开发者掌握如何在Java Web项目中高效地集成这三个核心框架,实现松耦合、可...

Global site tag (gtag.js) - Google Analytics