zoukankan      html  css  js  c++  java
  • SpringMVC配置

    1.导入必要的SpringMVC JAR

    2.在web.xml中配置SpingMVC的加载项

         <servlet>//SpringMVC配置加载项的方式为在web.xml中定义其加载的servlet

       <servlet-name>springMVC</servlet-name>//Servlet名称

       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

       //Servlet对应的class路径

       <init-param>//servlet的启动项

       <param-name>contextConfigLocation</param-name>//启动项的名称

       <param-value>classpath*:config/springAnnotation-servlet.xml</param-value>

                 //classpath*:表示在所有路径下加载

                //启动项的值,表示SpringMVC的配置文件为springAnnotation-servlet.xml

      //如果不配置此启动项,则默认寻找WEB-INF目录下名称为 [<servlet-name>]-servlet.xml,如springMVC-servlet.xml

       </init-param> 

        <load-on-startup>1</load-on-startup> 

      </servlet>

    <servlet-mapping>

       <servlet-name>springMVC</servlet-name>

       <url-pattern>/</url-pattern>

      </servlet-mapping>

    3.在src下建立config文件夹,在其中编写springAnnotation-servlet.xml文件

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"  

     xmlns:context="http://www.springframework.org/schema/context"  

     xmlns:p="http://www.springframework.org/schema/p"  

     xmlns:mvc="http://www.springframework.org/schema/mvc"  

     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  

          http://www.springframework.org/schema/context  

          http://www.springframework.org/schema/context/spring-context.xsd  

          http://www.springframework.org/schema/mvc  

          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!--<bean name="/test1/helloworld" class="com.tgb.web.controller.HelloWorldController" /> -->

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    <property name="prefix" value="/"></property>

    <property name="suffix" value=".jsp"></property>

    </bean>

     </beans>     

     

    <mvc:resources location=”/img/” mapping=”/img/**”/>这样就可以访问img下的静态文件。

     

     

    //配置多方法Controller需在SpringMVC配置文件中添加:

    <bean name="/test1/multi" class="com.tgb.web.controller.MultiController">

    <property name="methodNameResolver">

    <ref bean="paramMethodResolver"/>

    </property>

    </bean>

    <bean id="paramMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">

    <property name="paramName" value="action"></property>//value为解析的请求路径

    </bean>

    Java文件为:

     

    public class MultiController extends MultiActionController {

     

         public ModelAndView add(HttpServletRequest request,HttpServletResponse response){

          System.out.println("---add---");

          return new ModelAndView("/multi","method","add");

         }

         public ModelAndView update(HttpServletRequest request,HttpServletResponse response){

          System.out.println("---update---");

          return new ModelAndView("/multi","method","update");

         }

     

     

    启动注解时删除配置的bean和在配置多方法Controller需在SpringMVC配置文件中添加的代码,把SpringMVC的配置文件修改为如下:

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"  

     xmlns:context="http://www.springframework.org/schema/context"  

     xmlns:p="http://www.springframework.org/schema/p"  

     xmlns:mvc="http://www.springframework.org/schema/mvc"  

     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  

          http://www.springframework.org/schema/context  

          http://www.springframework.org/schema/context/spring-context.xsd  

          http://www.springframework.org/schema/mvc  

          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- 配置扫描包,启动服务器时需要扫描的包 -->

    <context:component-scan base-package="com.tgb.web.controller"></context:component-scan>

        <!-- 配置启动注解所需的两个bean -->

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>      

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    <property name="prefix" value="/"></property>

    <property name="suffix" value=".jsp"></property>

    </bean>

     </beans>  

     

     

     

     

     

     

     

  • 相关阅读:
    二分查找
    215. Kth Largest Element in an Array
    myeclipse导入web项目报错解决
    oracle替换字段中的空格
    sql变更表名
    cmd文件操作--- attrib命令
    oracle导入dmp文件
    java.lang.IllegalArgumentException: Page directive: invalid value for import 异常解决
    JDK_jvisualvm访问远程weblogic服务
    页面传值中文编码
  • 原文地址:https://www.cnblogs.com/fengweixin/p/3842546.html
Copyright © 2011-2022 走看看