继续记录学习过程欧~~ 昨天不小心把辛辛苦苦做的SpringMVC MyBatis PostgreSQL代码给删除掉了,哎~想undo,结果不允许 不过塞公失马焉知非福 今天再来一遍就是了,就当是巩固了,不过确实把一些遗留的问题给解决了。 昨天首先遇到的一个问题就是,由于配置文件路径不对,导致Web应用程序初始化错误,导致无法加载, 我还傻乎乎的通过浏览器访问,结果心灰意冷,因为以前都是初始化没有问题,真正访问程序的时候出问题, 此种情况还会在eclipse里面显示出错误信息再进行定位。但是现在什么错误信息都没有,直接访问不了了,就让人很担心, 后来调查发现,在启动tomcat服务器的时候,如果Web应用程序加载有问题,会出现错误信息,比如哪个配置文件无法读取之类的错误。 着实让我看到了生的希望,把问题给排除了。 OK,进入正题。 首先肯定是要创建动态web工程,加入web.xml,配置如下: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java./xml/ns/j2ee" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://java./xml/ns/j2ee http://java./xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/classes/config/applicationContext.xml</param-value> </context-param>
<listener> <listener-class>my.MyContextLoaderListener</listener-class> </listener> <!-- <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> -->
<servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <!-- 拦截所有以do结尾的请求 --> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app> 下面这段代码是为了创建全局的ApplicationContext用的。 <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/classes/config/applicationContext.xml</param-value> </context-param>
<listener> <listener-class>my.MyContextLoaderListener</listener-class> </listener> MyContextLoaderListener从ContextLoaderListener继承。 import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent;
import org.springframework.context.ApplicationContext; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.support.WebApplicationContextUtils;
public class MyContextLoaderListener extends ContextLoaderListener{
@Override public void contextInitialized(ServletContextEvent event){ super.contextInitialized(event); ServletContext context = event.getServletContext();
//获取web环境下的ApplicationContext ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
//将ApplicationContext,set到ContextUtil的静态变量context ContextUtil.setContext(ctx); } } 创建ContextUtil package my;
import org.springframework.context.ApplicationContext;
public class ContextUtil { private static ApplicationContext context;
public static ApplicationContext getContext() { return context; }
public static void setContext(ApplicationContext aContext) { context = aContext; } } 这样在需要的时候,就可以像下面这样访问代码,而不需要通过request等得到ServletContext再去获取了。 //ApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(req.getSession().getServletContext()); ApplicationContext ctx=ContextUtil.getContext(); UserMapper maper=(UserMapper)ctx.getBean(UserMapper.class); User user = maper.getUser("fff"); 下面来配置SpringMVC的配置文件dispatcherServlet-servlet.xml,我的理解是这样,呵呵 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www./dtd/spring-beans.dtd"> <beans> <!-- 定义映射 --> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="helloWorld.do">helloWorldAction</prop> </props> </property> </bean> <!-- 定义视图 --> <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="helloWorldAction" class="com.jp.action.HelloWorldAction"> <property name="helloWorld"> <value>Good Luck!</value> </property> <property name="viewPage"> <value>/index.jsp</value> </property> </bean> </beans> 我的理解就是把helloWorld.do映射到helloWorldAction里面去,再定义一下显示的方法。 package com.jp.action;
import java.util.HashMap; import java.util.Map; import java.util.logging.Logger; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import my.ContextUtil;
import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller;
import bean.User; import Mapper.UserMapper;
public class HelloWorldAction implements Controller{ private Logger logger=Logger.getLogger(this.getClass().getName()); private String helloWorld; private String viewPage;
public String getHelloWorld() { return helloWorld; }
public void setHelloWorld(String helloWorld) { this.helloWorld = helloWorld; }
public String getViewPage() { return viewPage; }
public void setViewPage(String viewPage) { this.viewPage = viewPage; }
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception { // TODO Auto-generated method stub //ApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(req.getSession().getServletContext()); ApplicationContext ctx=ContextUtil.getContext(); UserMapper maper=(UserMapper)ctx.getBean(UserMapper.class); User user = maper.getUser("fff"); Map model=new HashMap(); model.put("helloWorld",user.getName()); return new ModelAndView(getViewPage(),model); } } 这里在取显示数据的地方,是由映射器接口UserMapper,通过MyBatis连接PostgreSQL取得的。 但这个映射器接口UserMapper是通过Spring的注入生成。 来看看applicationContext.xml文件里是怎么注入UserMapper的。 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www./schema/beans" xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:p="http://www./schema/p" xmlns:context="http://www./schema/context" xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans-3.0.xsd http://www./schema/context http://www./schema/context/spring-context-3.0.xsd "> <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="org.postgresql.Driver"> </property> <property name="url" value="jdbc:postgresql:testdb"> </property> <property name="username" value="postgres"></property> <property name="password" value="nirvana7"></property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="myDataSource"/> <property name="configLocation" value="WEB-INF/classes/config/mybatis-config.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="Mapper"/> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="myDataSource"/> </bean> </beans> org.mybatis.spring.mapper.MapperScannerConfigurer会扫描Mapper的package,然后生成接口,可以参照前篇文章来了解。 到这里主要流程都已经搞定了,很多麻烦的地方在于配置文件的路径问题,很是让人烦恼啊。 代码上传上去。 《《源码地址下载
|