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>
 
        

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM