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

来源:互联网 发布:手机焓湿图计算软件 编辑:程序博客网 时间:2024/06/05 08:31

<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 {@Autowiredprivate 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;@Servicepublic 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.Driverjdbc_url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8jdbc_username=testjdbc_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;@Servicepublic 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";}







0 0