SpringMVC-Eclipse创建一个最简单的web项目

  1. 导入Spring相应的jar包

  2. 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" 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>jstest</display-name>
      <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>
    
        <!-- 第1步:配置DispatchcerServlet 前端过滤器配置-->
    	<servlet>
    		<servlet-name>mySpring</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>classpath:mvc.xml</param-value>
    		</init-param>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    	<!-- 主要是截获请求的,如果你的url-pattern定义的是路径,那么以后所有对这个路径下资源的请求都会由servlet-name中定义的servlet处理 -->       
        <!-- 如果你的url-pattern定义的是资源格式例如*.do等,那么对于所有符合这种格式的资源的请求都由指定的  -->
        <!-- 如果你的url-pattern定义的是资源格式例如*.do等,那么对于所有符合这种格式的资源的请求都由指定的servlet处理 -->
    	<servlet-mapping>
    		<servlet-name>mySpring</servlet-name>
    		<url-pattern>/</url-pattern>
    	</servlet-mapping>
    	<!-- SpringMVC中的控制器(org.springframework.web.servlet.DispatcherServlet)中默认是jsp页面,所以如果返回跳转到html页面需要加一个default配置 -->
    	<servlet-mapping>
    	    <servlet-name>default</servlet-name>
    	    <url-pattern>*.html</url-pattern>
    	</servlet-mapping>
    
        <!-- 第2步:配置 spring-web-4.3.9.RELEASE.jar 提供的监听器,可以在服务器启动时初始化 IoC 容器 -->
    	<!-- 启动Web容器时,自动装配ApplicationContext的配置信息(context-param指定配置文件地址) -->
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    	<!-- 指定spring配置文件的位置 -->
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath:applicationContext.xml</param-value>
    	</context-param>
    	
    	<!-- filter编码过滤 解决springMVC的post乱码 -->
    	<filter>
    		<filter-name>encodingFilter</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>encodingFilter</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
    
        <!--shiro 暂不考虑-->
    	<!--代理过滤器,该代理过滤器会从spring容器中找shiroFilter-->
    	<!-- <filter>
    		<filter-name>shiroFilter</filter-name>
    		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    	</filter>
    	<filter-mapping>
    		<filter-name>shiroFilter</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>  -->
    </web-app>
  3. mvc.xml配置

    <!-- mvc.xml中的标准配置 -->
    <!-- 至此,这里配置好之后可以直接http://localhost:8080/SpringMVCDemo/hello 来访问后端Controller-->
    <?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"
    	xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    
    	<!-- 配置自动扫描的包 也可以在application.xml中配置-->
    	<context:component-scan base-package="cn.test.ry"></context:component-scan>
    
        <!--引入其他配置文件,在web.xml中引入了applicationContext.xml 这里先注释掉-->
        <!-- <import resource="classpath:applicationContext.xml"/> -->
        <!--引入shiro的配置文件暂时不考虑shiro内容-->
        <!-- <import resource="classpath:myShiro.xml"/> -->
     
        <!--配置MVC注解驱动器-->
        <mvc:annotation-driven/>
        <!-- 处理静态资源 我理解的css js类资源,有待再确定??-->
        <mvc:default-servlet-handler/>
    
    	<!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
    	<!--prefix和suffix:查找视图页面的前缀和后缀(前缀[逻辑视图名]后缀), -->
    	<!-- 比如传进来的逻辑视图名为result,则该该jsp视图页面应该存放在“/WEB-INF/result.jsp” -->
    	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix" value="/WEB-INF/static"></property>
    		<property name="suffix" value=".html"></property>
    	</bean>
    
        <!--配置freeMarker的模板路径,配置自定义的配置类,
            将MyFreeMarkerConfig设置成当前环境中使用的配置对象,
            让FreeMarker去解析shiro的标签以控制页面显示(在前端页面上,根据用户拥有的权限来显示具体的页面)-->
        <!-- <bean class="cn.xbao.pet.util.MyFreeMarkerConfig">
            配置freemarker的文件编码
            <property name="defaultEncoding" value="UTF-8" />
            配置freemarker寻找模板的路径
            <property name="templateLoaderPath" value="/WEB-INF/views/" />
        </bean> -->
     
        <!--freemarker视图解析器 -->
        <!-- <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
            是否在model自动把session中的attribute导入进去;
            <property name="exposeSessionAttributes" value="true" />
            配置逻辑视图自动添加的后缀名
            <property name="suffix" value=".ftl" />
            配置视图的输出HTML的contentType
            <property name="contentType" value="text/html;charset=UTF-8" />
        </bean> -->
     
        <!--统一异常处理器方式一-->
        <!-- <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
            定义默认的异常处理页面,当该异常类型的注册时使用
            异常出错,默认跳转至错误视图
            <property name="defaultErrorView" value="common/error"/>
            定义异常处理页面用来获取异常信息的变量名,默认名为exception
            <property name="exceptionAttribute" value="ex"/>
            定义需要特殊处理的异常,用类名或完全路径名作为key,异常也页名作为值
            <property name="exceptionMappings">
                <value>
                    org.apache.shiro.authz.UnauthorizedException=common/nopermission
                    这里还可以继续扩展不同异常类型的异常处理
                </value>
            </property>
        </bean> -->
    </beans>
  4. applicationContext.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"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/context
    		http://www.springframework.org/schema/context/spring-context.xsd
    		http://www.springframework.org/schema/aop
    		http://www.springframework.org/schema/aop/spring-aop.xsd
    		http://www.springframework.org/schema/tx
    		http://www.springframework.org/schema/tx/spring-tx.xsd">
     
        <!--组件扫描器-->
        <context:component-scan base-package="cn.test.ry"/>
     
        <!-- db.properties通过属性展位符交给Spring管理 -->
        <!-- 使用属性占位符,读取属性文件 -->
        <!-- <context:property-placeholder location="classpath:db.properties"
                                      system-properties-mode="NEVER"/> -->
        <!-- 配置这个表示环境变量不被别的覆盖 -->
     
        <!-- 把德鲁伊连接池交给Spring管理 -->
        <!-- 连接池要配初始化方法和销毁方法 -->
        <!-- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
              init-method="init" destroy-method="close">
            设置数据库4要素
            在德鲁伊连接池里面,第一个属性驱动类名可以不配,因为它可以根据url属性来自动识别是哪个数据库,
            	这里可以识别到器数据库是mysql,则其驱动类名就是com.mysql.jdbc.Drive
            <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> -->
     
        <!-- 配置业务层组件:将业务层组件Service的实现类交给Spring管理 ,注:id默认的,可写可不写-->
        <!-- 1:把SqlSessionFactory交给Spring管理 -->
        <!-- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            1:连接池
            <property name="dataSource" ref="dataSource"/>
            2:管理的mapper.xml文件
            这里不直接用EmployeeMapper.xml而用*Mapper.xml是因为不想把它写死!
            <property name="mapperLocations" value="classpath:cn/xbao/pet/mapper/*Mapper.xml"/>
     
            注意:1和2都是必配的,,以下3和4时选配的(根据需求,甚至可以不配)
            3:包的别名扫描,比如要写类的简单名称,那么就要配一个别名,如果没配这个别名,就不可以写别名,它默认是全限定名
            <property name="typeAliasesPackage" value="cn.test.ry.domain"/>
            4:框架的个性配置:比如Mybatis个性配置:懒加载
            引入mybatis的配置文件
            <property name="configLocation" value="classpath:mybatis.xml"/>
        </bean> -->
     
        <!-- 2:配置mapper接口扫描器 -->
        <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            扫描哪个包中的接口
            <property name="basePackage" value="cn.test.ry.mapper"/>
        </bean> -->
     
        <!--声明式事务管理-->
        <!-- AOP:3W -->
        <!-- what:做什么增强 -->
        <!-- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            开启事务: conn.setAutoCommit(false);要执行这个代码就要有Connection对象,而该对象是从连接池来的
            管理哪个连接池的事务
            <property name="dataSource" ref="dataSource"/>
        </bean> -->
     
     
        <!-- <aop:config>
            where:在哪里做增强
            <aop:pointcut id="pc" expression="execution(* cn.test.ry.service.impl.*ServiceImpl.*(..))"/>
            三剑客连接:where和when连接在一起 
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
        </aop:config> -->
     
        <!--
            when:在什么时机做增强(底层是自定义增强)
            	由tx:advice标签底层封装好了,是环绕增强
            		并且when和what连接在一起
         -->
        <!-- <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                对方法做个性事务配置
                DQL事务管理
                <tx:method name="get*" read-only="true"/>
                <tx:method name="list*" read-only="true"/>
                <tx:method name="query*" read-only="true"/>
                DML事务管理
                <tx:method name="*"/>
            </tx:attributes>
        </tx:advice> -->
     
     
        <!-- 配置Mapper接口的代理对象 -->
        <!-- <bean id="accountMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
            配置SqlSessionFactory来源
            <property name="sqlSessionFactory" ref="sqlSessionFactory" />
            代理哪个接口
            <property name="mapperInterface" value="cn.xbao.mybatis.mapper.AccountMapper" />
        </bean> -->
    </beans>

  • 新建web项目的时候遇到的问题:

    • 在打算把前后端数据交互做一下的时候,正常引用时js和css一直报404,是因为使用了如下配置:(引入静态文件)    

    • <mvc:resources mapping="/layui/**" location="/layui/"/>
      
    • 然后换成这种写法就好了

    • <mvc:default-servlet-handler/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值