IDEA中springmvc的配置

第一种方式:

利用DispatcherServlet的一个初始化参数contextConfigLocation来配置springmvc配置文件的位置和名称

 

1.配置springmvc配置文件的位置和名称

  打开web.xml,具体配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

<!--配置DispatcherServlet-->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--配置DispatcherServlet的一个初始化参数:配置springmvc配置文件的位置和名称-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <!--这里的“1”指明:该servlet是在web应用被加载就被创建,而不是第一次请求才被创建-->
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern><!--这里的/表示可以应答所有请求-->
        <!--<url-pattern>*.form</url-pattern>-->
    </servlet-mapping>

</web-app>

2.在src下创建springmvc.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
>

    <!--配置自动扫描的包-->
    <context:component-scan  base-package="com.cjx.demo"></context:component-scan>

    <!--配置视图解析器:如何把handler方法返回值解析为实际的物理视图-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

这里的prefix和suffix为controller层返回页面路径加的前缀和后缀

如   Controller层某接口返回 :“success”

  则系统默认为转到  /WEB-INF/views/success.jsp 页面

第二种方式:使用默认的springmvc配置文件

默认的配置文件为:
/WEB-INF/<servlet-name>-servlet.xml
此时web.xml中代码如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
 <!--配置DispatcherServlet-->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

     
        <!--实际上也可以不通过contextConfigLocation来配置springMVC的配置文件
            而是使用默认的配置文件
            默认的配置文件为:
            /WEB-INF/<servlet-name>-servlet.xml
            这里的<servlet-name>为dispatcher,所以默认的配置文件为/WEB-INF/dispatcher-servlet.xml
        -->
        <load-on-startup>1</load-on-startup>
        <!--这里的“1”指明:该servlet是在web应用被加载就被创建,而不是第一次请求才被创建-->
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern><!--这里的/表示可以应答所有请求-->
        <!--<url-pattern>*.form</url-pattern>-->
    </servlet-mapping>
</web-app>

分析如下:

dispatcher-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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--配置自动扫描的包-->
    <context:component-scan  base-package="com.cjx.demo"></context:component-scan>

    <!--配置视图解析器:如何把handler方法返回值解析为实际的物理视图-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

 

 

 

posted @ 2018-08-22 13:57  薪薪点灯  阅读(3421)  评论(0编辑  收藏  举报