1.在tomcat图形界面里创建jndi数据源.(Tomcat Administration菜单里)
2.在对应的web应用工程下绑定该连接源.例如假设该Web应用项目名为SpringMVC.则在Tomcat5.X系列下的对应配置文件名(包含指定的路径:C:\Tomcat 5.0\conf\Catalina\localhost\SpringMVC.xml)
3.SpringMVC.xml内容为:
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="E:/workspace/SpringMVC" path="/SpringMVC" reloadable="true" workDir="E:\workspace\SpringMVC\work">
<!--Resource auth="Container" name="jdbc/spring" type="javax.sql.DataSource"/-->
<!--应用工程,指定绑定的jndi-->
<ResourceLink name="jdbc/spring" global="jdbc/spring" type="javax.sql.DataSource" />
</Context>
4.Tomcat5.x下的配置数据源在,server.xml文件中.
5.Spring利用该连接源,进行数据库操作.(如何配置)
1.在该Web应用工程下的web.xml中配置Spring控制台
2.web.xml文件的内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springappservlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<!-- 字母区分大小写-->
<url-pattern>/servlet/HelloWorld</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!--(过滤器) 对提交的数据进行统一的编码-->
<filter>
<filter-name>encoding</filter-name>
<filter-class>EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3.创建对应的spring配置即Web.xml中指定的目录下的配置文件>/WEB-INF/springappservlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!-- SelectMethod=cursor :打开游标-->
<!-- 用SQL Server驱动一次select很多数据最好在
connection string中加上SelectMethod=Cursor,
以利用服务器端游标加快速度,其实不只sqlserver,
oracle的jdbc,只要使用PreparedStatement,
驱动默认就使用游标,sqlserver则不然,
必须使用SelectMethod=Cursor才打开游标-->
<beans>
<!-- bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
<property name="driverClassName">
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</property>
<property name="url">
<value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Northwind;SelectMethod=cursor</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value>628418</value>
</property>
</bean-->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/spring</value>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="userDAO" class="UserDAO">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate"/>
</property>
</bean>
<bean id="userAction" class="UserAction">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate"/>
</property>
<property name="commandClass">
<value>UserInfo</value>
</property>
<property name="fail_view">
<value>/WEB-INF/jsp/fail.jsp</value>
</property>
<property name="success_view">
<value>/WEB-INF/jsp/success.jsp</value>
</property>
</bean>
<bean id="springappController" class="SpringappController"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.InternalResourceView</value>
</property>
</bean>
<bean id="helloUserAction" class="HelloUserAction">
<property name="helloWord">
<value>Hello!</value>
</property>
<property name="viewPage">
<value>/WEB-INF/jsp/hellouser.jsp</value>
</property>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<!-- 控制器模式-->
<property name="mappings">
<props>
<prop key="/hello.do">springappController</prop>
<prop key="/hellouser.do">helloUserAction</prop>
<prop key="/insert.do">userAction</prop>
</props>
</property>
<!-- 拦截器模式>
<property name="interceptors">
<list>
<ref local="localeChangeInterceptor"/>
<ref local="themeChangeInterceptor"/>
<ref local="copyInterceptor"/>
</list>
</property-->
</bean>
</beans>
4.对应的应用UserAction
import java.io.UnsupportedEncodingException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
public class UserAction extends SimpleFormController {
private String fail_view;
private String success_view;
private JdbcTemplate jdbcTemplate;
protected ModelAndView onSubmit(Object cmd, BindException ex)
throws Exception {
System.out.println("--------go into UserAction-------");
UserInfo userInfo = (UserInfo) cmd;
System.out.println("name=" + userInfo.getName() + ",sex="
+ userInfo.getSex() + ",age=" + userInfo.getAge());
if (login(userInfo) == 1) {
insertUser(userInfo);
return new ModelAndView(this.success_view);
}
return new ModelAndView(this.fail_view);
}
public void insertUser(UserInfo user) throws UnsupportedEncodingException {
System.out.println("-----------------");
System.out.println("name=" + user.getName() + ",sex=" + user.getSex()
+ ",age=" + user.getAge());
System.out.println("-----------------");
//if(user.getName()!=null && !user.getName().equals(""))
//user.setName(new String(user.getName().getBytes("ISO8859_1"),"GBK"));
System.out.println("name=" + user.getName() + ",sex=" + user.getSex()
+ ",age=" + user.getAge());
jdbcTemplate.update("INSERT INTO USERS VALUES('" + user.getSex()
+ "', '" + user.getName() + "', '" + user.getAge() + "')");
}
private int login(UserInfo userInfo) {
if (userInfo.getName().equals("李冲")) {
System.out.println("11");
return 0;
} else
return 1;
}
public String getFail_view() {
return fail_view;
}
public void setFail_view(String fail_view) {
this.fail_view = fail_view;
}
public String getSuccess_view() {
return success_view;
}
public void setSuccess_view(String success_view) {
this.success_view = success_view;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}