弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的
spring3.0 mvc和rest小例子
没有介绍到数据层的内容,现在这一篇补上。下面开始贴代码。
文中用的框架版本:spring 3,hibernate 3,没有的,自己上网下。
web.xml配置:
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
<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_2_5.xsd" id="WebApp_ID" version="2.5">
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
<display-name>s3h3</display-name>
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
<context-param>
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
<param-name>contextConfigLocation</param-name>
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
<param-value>classpath:applicationContext*.xml</param-value>
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
</context-param>
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
<listener>
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
<listener-
class>org.springframework.web.context.ContextLoaderListener</listener-
class>
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
</listener>
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
<servlet>
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
<servlet-name>spring</servlet-name>
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
<servlet-
class>org.springframework.web.servlet.DispatcherServlet</servlet-
class>
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
<load-on-startup>1</load-on-startup>
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
</servlet>
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
<servlet-mapping>
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
<servlet-name>spring</servlet-name> <!-- 这里在配成spring,下边也要写一个名为spring-servlet.xml的文件,主要用来配置它的controller -->
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
<url-pattern>*.
do</url-pattern>
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
</servlet-mapping>
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
<welcome-file-list>
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
<welcome-file>index.jsp</welcome-file>
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
</welcome-file-list>
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
</web-app>
spring-servlet,主要配置controller的信息
<?
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:p
="http://www.springframework.org/schema/p"
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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
>
<
context:annotation-config
/>
<!--
把标记了@Controller注解的类转换为bean
-->
<
context:component-scan
base-package
="com.mvc.controller"
/>
<!--
启动Spring MVC的注解功能,完成请求和注解POJO的映射
-->
<
bean
class
="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"
/>
<!--
对模型视图名称的解析,即在模型视图名称添加前后缀
-->
<
bean
class
="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix
="/WEB-INF/view/"
p:suffix
=".jsp"
/>
<
bean
id
="multipartResolver"
class
="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding
="utf-8"
/>
</
beans
>
applicationContext.xml代码
hibernate.properties数据库连接配置
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
dataSource.password=123
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
dataSource.username=root
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
dataSource.databaseName=test
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
dataSource.driverClassName=com.mysql.jdbc.Driver
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
dataSource.dialect=org.hibernate.dialect.MySQL5Dialect
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
dataSource.serverName=localhost:3306
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
dataSource.url=jdbc:mysql://localhost:3306/test
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
dataSource.properties=user=${dataSource.username};databaseName=${dataSource.databaseName};serverName=${dataSource.serverName};password=${dataSource.password}
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
dataSource.hbm2ddl.auto=update
配置已经完成,下面开始例子
先在数据库建表,例子用的是MySQL数据库
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
CREATE TABLE `test`.`student` (
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
`name` varchar(45) NOT NULL,
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
`psw` varchar(45) NOT NULL,
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
PRIMARY KEY (`id`)
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
)
建好表后,生成实体类
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
package com.mvc.entity;
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
import java.io.Serializable;
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
import javax.persistence.Basic;
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
import javax.persistence.Column;
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
import javax.persistence.Entity;
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
import javax.persistence.GeneratedValue;
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
import javax.persistence.GenerationType;
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
import javax.persistence.Id;
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
import javax.persistence.Table;
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
@Entity
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
@Table(name = "student")
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
public class Student implements Serializable {
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
private static final long serialVersionUID = 1L;
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
@Id
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
@Basic(optional = false)
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
@GeneratedValue(strategy = GenerationType.IDENTITY)
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
@Column(name = "id", nullable = false)
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
private Integer id;
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
@Column(name = "name")
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
private String user;
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
@Column(name = "psw")
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
private String psw;
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
public Integer getId() {
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
return id;
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
}
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
public void setId(Integer id) {
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
this.id = id;
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
}
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
public String getUser() {
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
return user;
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
}
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
public void setUser(String user) {
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
this.user = user;
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
}
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
public String getPsw() {
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
return psw;
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
}
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
public void setPsw(String psw) {
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
this.psw = psw;
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
}
![](http://img.e-com-net.com/image/info5/bdecc7a8941d4a0480b17667d118af43.gif)
}
Dao层实现
Dao在applicationContext.xml注入
<
bean
id
="entityDao"
class
="com.mvc.dao.EntityDaoImpl"
>
<
property
name
="sessionFactory"
ref
="sessionFactory"
/>
</
bean
>
Dao只有一个类的实现,直接供其它service层调用,如果你想更换为其它的Dao实现,也只需修改这里的配置就行了。
开始写view页面,WEB-INF/view下新建页面student.jsp,WEB-INF/view这路径是在spring-servlet.xml文件配置的,你可以配置成其它,也可以多个路径。student.jsp代码
student_add.jsp
controller类实现,只需把注解写上,spring就会自动帮你找到相应的bean,相应的注解标记意义,不明白的,可以自己查下@Service,@Controller,@Entity等等的内容。
service类实现
OK,例子写完。有其它业务内容,只需直接新建view,并实现相应comtroller和service就行了,配置和dao层的内容基本不变,也就是每次只需写jsp(view),controller和service调用dao就行了。
怎样,看了这个,spring mvc是不是比ssh实现更方便灵活。