標籤:
繼續記錄學習過程歐~~
昨天不小心把辛辛苦苦做的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.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/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.springframework.org/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.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/context
http://www.springframework.org/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,然後產生介面,可以參照前篇文章來瞭解。
到這裡主要流程都已經搞定了,很多麻煩的地方在於設定檔的路徑問題,很是讓人煩惱啊。
》》源碼地址擷取
springmvc + mybatis整合詳細,及遇到的問題請參看以下資料:
參考資料:
http://www.springmvc,net/detail/6074493.html
http://my.spring.net/wangbiglei/blog/489583
http://my.springmvc.net/wangbiglei/blog/489604
SpringMVC MyBatis PostgreSQL整合