使用springMVC框架的相關配置文件

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">第一次寫,如有差錯煩請指出;</span>

最近因爲公司沒事幹自己學習搭建框架,因以前所接觸的都是金融類項目,所用框架都是ssh;springMVC對於本人來說還是比較陌生的;所以就想通過搭建框架來認識它;

第一步:準備jar包;(這些jar都是我從網上東拼西湊合在一起的,有些可能是多餘的)

jar包目錄:



























第二步:配置web.xml;

<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>test_005</display-name>
  
  <!--不初始化servlet.xml;系統會自動在WEB-INF下面找  servlet-name 加 -servlet.xml  -->
  <servlet>
    <servlet-name>springTest</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <description>核心配置</description>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/springTest-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 跳轉路徑設置 -->
  <servlet-mapping>
    <servlet-name>springTest</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
  
  
  
  
  <!-- 配置編碼轉換  UTF-8 -->
  <filter>
    <filter-name>encodeingFilter</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>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodeingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  
  <!-- 配置異常跳轉頁面 -->
  <error-page>
    <error-code>404</error-code>
    <location>/err/404.jsp</location>
  </error-page>
  <error-page>
    <error-code>500</error-code>
    <location>/err/500.jsp</location>
  </error-page>
  <error-page>
    <error-code>401</error-code>
    <location>/err/401.jsp</location>
  </error-page>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

第三步:web.xml文件配置完成之後,開始配置-servlet.xml文件

<?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"
    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">
    
    <!-- 對相關包下面的類進行掃描,進行自動注入 -->
    <context:component-scan base-package="com.test.controller,com.test.service"/>
    <!-- 進行請求映射  controller -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
    
    <!-- 配置視圖解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/jsp/"/>
      <property name="suffix" value=".jsp"/>
    </bean>
    
</beans>

4、servlet配置完成之後開始編寫controller和service ,jsp開始進行測試;


controller類:

package com.test.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.test.service.TestService;

@Controller
@RequestMapping(value="/test/controller")
public class TestController {
	
	@Autowired
	private TestService testService;
	
	@RequestMapping(value="test1")
	public String test1(Model model){
		model.addAttribute("test1", testService.test1());
		return "test1";
	}

}
service類:

package com.test.service;

import org.springframework.stereotype.Service;

@Service
public class TestService {
	
	public String test1(){
		return "嘿嘿嘿...可以了!!!";
	}

}
以下均爲jsp頁面body

index.jsp頁面:

<h4>開始測試:</h4>
<a href="test/controller/test1">去test1頁面</a>
404.jsp頁面:
<h4>路徑出錯鳥!!!!</h4>

test1.jsp頁面:

<h4>我是test1</h4>
		${test1 }
放入tomcat裏面,開始運行;

OK運行完成沒有問題;

最簡單的地方搞好了;現在開始配置數據庫連接,dao等等信息;

第四步:將web.xml文件添加 spring監聽,也就是項目啓動時自動讀取上下文;

 <!-- 配置監聽,讀取上下文 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:resource/applicationContext-*.xml</param-value>
  </context-param>
現在開始寫配置文件:jdbc、tx等等信息;

applicationContext-dao.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
    <bean id="myTestMapper" parent="baseMapper">
        <property name="mapperInterface" value="com.test.dao.MyTestMapper" />
    </bean>
  
</beans>


applicationContext-jdbc.xml:

<?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:jee="http://www.springframework.org/schema/jee"
  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/jee 
  http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  
  <!-- 數據源配置,使用應用內的dbcp -->
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc_driver}"/>
    <property name="url" value="${jdbc_url}"/>
    <property name="username" value="${jdbc_username}"/>
    <property name="password" value="${jdbc_password}"/>
  </bean>
  
  <bean id="sessionFactroy" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
       <property name="mapperLocations">
          <list>
            <value>classpath*:com/test/mapper/*.xml</value>
          </list>
       </property>
  </bean>
  
  <bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true">
    <property name="sqlSessionFactory" ref="sessionFactroy"/>
  </bean>
  
</beans>


applicationContext-resource.xml:

<?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:jee="http://www.springframework.org/schema/jee"
  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/jee 
  http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  
  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
      <list>
        <value>classpath*:resource/mysql-jdbc.properties</value>
      </list>
    </property>
  </bean>
  
</beans>


applicationContext-tx.xml:

<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-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/jee 
  http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <!-- 配置Hibernate事務管理器 -->
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
   
   <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
      <property name="transactionManager" ref="transactionManager"/>
      <property name="transactionAttributes">
        <props>
          <prop key="save*">PROPAGATION_REQUIRED</prop>
          <prop key="update*">PROPAGATION_REQUIRED</prop>
          <prop key="dele*">PROPAGATION_REQUIRED</prop>
          <prop key="insert*">PROPAGATION_REQUIRED</prop>
          <prop key="add*">PROPAGATION_REQUIRED</prop>
        </props>
      </property>
   </bean>
   
   <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
      <property name="beanNames">
        <list>
          <value>*Service</value>
        </list>
      </property>
      <property name="interceptorNames">
        <list>
          <value>transactionInterceptor</value>
        </list>
      </property>
   </bean>
</beans>


mysql-jdbc.properties:
jdbc_driver=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8
jdbc_username=test
jdbc_password=test


dao類:

package com.test.dao;

public interface MyTestMapper {

List<MyTest> selectByMyTestInfo();

}


service類:

package com.test.service;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.test.dao.MyTestMapper;
import com.entity.MyTest;

@Service
public class TestService {
	@Autowired
<span style="white-space:pre">	</span>private MyTestMapper myTestMapper;
	public String test1(){
		return "嘿嘿嘿...可以了!!!";
	}

<span style="white-space:pre">	</span>public List<MyTest> selectByMyTestInfo(){
		return myTestMapper.selectByMyTestInfo();
	}

}

mapper.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.dao.MyTestMapper" >
  <resultMap id="BaseResultMap" type="com.entity.MyTest" >
    <id column="ID" property="id" jdbcType="BIGINT" />
    <result column="txt" property="txt" jdbcType="VARCHAR" />
    <result column="desc" property="desc" jdbcType="VARCHAR" />
   
  </resultMap>
  <sql id="Base_Column_List" >
    id, txt, desc
  </sql>
  <select id="selectByMyTestInfo" resultMap="BaseResultMap">
    select * from myTest
  </select>
</mapper>


controller類添加

<span style="white-space:pre">	</span>@RequestMapping(value="method1")
	public String show(Model model){
		List<MyTest> list=myService.selectByMyTestInfo();
		StringBuffer sb=new StringBuffer();
		for(MyTest my: list){
			sb.append("id="+my.getId()+"; txt="+my.getTxt()+";  desc="+my.getDesc()+"<br/>");
		}
		model.addAttribute("sbToString", sb);
		return "show";
	}







發佈了36 篇原創文章 · 獲贊 34 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章