源码下载地址:http://download.csdn.net/detail/mexican_jacky/9333525
本文是自己在学习SpringMVC做一个简单记录,方便以后自己忘记回来查询。
第一步:配置DispatcherServlet以及字符编码处理
<?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">
<!--
在web.xml中配置DispatcherServlet 此处就相当于Struts2的拦截器一样
-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载的时候就启动 -->
<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>
</web-app>
第二步:新建SpringMVC控制器,注明:在新建SpringMVC控制器的文件名必须与web.xml中DispatcherServlet中的servletName一致
SpringMVC-servlet中的配置文件信息如下:
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!-- 注解的方式 -->
<!-- Controller基于那个包 -->
<context:component-scan base-package="org.ksea.controller"/>
<mvc:annotation-driven/>
<!-- 将静态文件指定到某个特殊的文件夹中统一处理 /resources/**表示 resources文件夹中所有文件 -->
<mvc:resources location="/resources/" mapping="/resources/**"/>
<!-- 全局异常处理 -->
<bean id="exceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<!-- prop中 key是您定义的异常类作为key,value是您要跳转异常处理的页面 这里就是 error.jsp -->
<prop key="org.ksea.exception.UserExcetion" >user/error</prop>
</props>
</property>
</bean>
<!-- 文件上传配置 设置CommonsMultipartResolver才能完成上传-->
<bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 限制文件大小为5M -->
<property name="maxUploadSize" value="5000000"></property>
</bean>
<!-- 映射视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 视图前缀 -->
<property name="prefix" value="/WEB-INF/pages/"/>
<!-- 视图后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
-----------------------以上为SpringMVC框架的一个简单配置---------------------------------------------
以下实例采用原生态JBDC进行测试:
package org.ksea.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.text.html.HTMLDocument.HTMLReader.PreAction;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
public class DatabaseUtil {
private static final String USER_NAME = "root";
private static final String USER_PASSWORD = "root";
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static MysqlDataSource dataSource;
static {
dataSource = new MysqlDataSource();
dataSource.setDatabaseName("ksea");
dataSource.setPort(3306);
dataSource.setUser(USER_NAME);
dataSource.setPassword(USER_PASSWORD);
}
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
public static void executeUpdate(String sql, Object... params) {
try {
Connection con = getConnection();
PreparedStatement statement = con.prepareStatement(sql);
if (null != params && params.length > 0) {
for (int i = 0; i < params.length; i++) {
statement.setObject(i + 1, params[i]);
}
}
statement.executeUpdate();
closeResource(null, statement, con);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static ResultSet executeQuery(String sql, Object... params) {
try {
Connection con = getConnection();
PreparedStatement statement = con.prepareStatement(sql);
if (null != params && params.length > 0) {
for (int i = 0; i < params.length; i++) {
statement.setObject(i + 1, params[i]);
}
}
return statement.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
// 关闭连接,此方法主要针对增,删,改的关闭
private static void closeResource(ResultSet resultSet, Statement statement,
Connection connection) {
try {
if (null != resultSet) {