从头开始学习JAVA 5--Spring MVC

4 Spring MVC

1)引入Jar

IVSMDemo中,引入如下jar包:

 从头开始学习JAVA 5--Spring MVC_第1张图片

从头开始学习JAVA 5--Spring MVC_第2张图片

注意引入Java EE 5 Library

2)配置Web.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appversion="2.5"

    xmlns="http://java.sun.com/xml/ns/javaee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 <!--显示标题 -->

  <display-name>xmr_Spring_Demo</display-name>

     

    <listener>

        <listener-class>

            org.springframework.web.context.ContextLoaderListener

        </listener-class>

    </listener>

 

    <!-- Handles all requests into the application -->

    <!--设置Spring配置文件路径 -->

    <servlet>

        <servlet-name>springServlet</servlet-name>

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

        <init-param>

            <param-name>contextConfigLocation</param-name>

            <param-value>

                /WEB-INF/spring/springContext.xml

            </param-value>

        </init-param>

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

    </servlet>

    <!--Spring mvc处理的资源(action -->

    <servlet-mapping>

        <servlet-name>springServlet</servlet-name>

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

    </servlet-mapping>

</web-app>

 

注意:

要在WebRoot\WEB-INF下,添加默认的程序配置文件applicationContext.xml,或者在web.xml中指定。

applicationContext.xml的内容:

<?xmlversion="1.0"encoding="UTF-8"?>

 

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

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:jaxws="http://cxf.apache.org/jaxws"

    xsi:schemaLocation="http://www.springframework.org/schema/beans 

                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

                        http://cxf.apache.org/jaxws

                        http://cxf.apache.org/schemas/jaxws.xsd">

</beans>

Web.xml中指定程序配置文件:

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

<web-app version="2.5"

    xmlns="http://java.sun.com/xml/ns/javaee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>

            /WEB-INF/webservice.xml

        </param-value>

    </context-param>

</web-app>

3Spring配置文件

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd

       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

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

 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->

    <mvc:resourcesmapping="/resources/**"location="/resources/"/>

   

    <!-- Scans the classpath of this application for @Components to deploy as beans -->

    <context:component-scanbase-package="com.ivsm.controller"/>

   

    <!-- Configures the @Controller programming model 配置控制器模块-->

    <mvc:annotation-driven/>

   

    <!--local 保存在cookie -->

    <beanid="localeResolver"class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>

 

    <!--组成视图的路径,保护目录/WEB-INF/views下的.jsp资源 -->

    <beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">

       <propertyname="prefix"value="/WEB-INF/views/"/>

       <propertyname="suffix"value=".jsp"/>

    </bean>

   

    <!--欢迎页面 -->

    <mvc:view-controllerpath="/"view-name="index"/>

</beans>

schemaLocation

注意schemaLocation要与jar的版本一致,否则会导致在无网络的情况下,找不到Schem

组成视图的路径

mvc:resources

context:component-scan

控制器包规则,符合该规则包中的类认为是控制器类(控制器类以@Controller标注)。

4)添加JSP页面

根据组成视图路径的规则,在“WEB-INF/views/”下,创建一个JSP页面(Index.jsp

从头开始学习JAVA 5--Spring MVC_第3张图片

5)添加控制类

根据配置,添加com.ivsm.controller包下的控制类。

从头开始学习JAVA 5--Spring MVC_第4张图片

为该类添加“@Controller”标签,表名该类是一个控制类;

添加“@RequestMapping”标签(例如:@RequestMapping(value = "/MyTest/*")),该控制类处理来自/user/下的请求。

添加init函数,来为Index页面进行初始化,返回值为jsp页面的路径(去掉/WEB-INF/views/.jsp

@Controller

@RequestMapping(value ="/MyTest/*")

publicclass UserCT {

     /**

     *初始化页面

     *

     *@return前台转向

     */

    @RequestMapping(value ="default")

    public String init(ModelMap mdlMap) {

 

        return"user/Index";

}

}

该函数表示,对Web请求(”/MyTest/default”)映射到(/WEB-INF/views/user/Index. jsp)页面。

类代码:

package com.ivsm.controller.Web;

 

import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.RequestMapping;

 

@Controller

@RequestMapping(value ="/MyTest/*")

publicclass UserCT {

   

     /**

     *初始化道路管理页面

     *

     *@return前台转向

     */

    @RequestMapping(value ="default")

    public String init(ModelMap mdlMap) {

 

        return"user/Index";

    }

}

6)访问页面

运行IVSMWebDemo,访问http://localhost:8080/IVSMWebDemo/MyTest/default

 

 

注:

我的很多地方并不规范,因为初次接触的缘故,高手告诉我,所有源代码最好放在src/main/java下面,资源放在src/main/resources下面,逻辑代码以*Service命名,数据库接口以*Mapper命名,请求对象以*Vo命名等。

 

你可能感兴趣的:(java,spring,mvc,MyEclipse)