SpringMVC的基本使用步骤

SpringMVC的基本使用步骤

1.创建web工程

2.在src下创建lib文件夹,导入SpringMVC相关jar包(在Spring的基础之上再添加一个mvc包)

 3.在工程文件夹下创建resource文件夹,并添加配置文件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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <!--开启注解扫描-->
    <context:component-scan base-package="com.helong"/>

    <!--视图解析器前/后缀设置-->
    <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        &lt;!&ndash;设置前缀&ndash;&gt;
        <property name="prefix" value="/WEB-INF/helong/"/>
        &lt;!&ndash;设置后缀&ndash;&gt;
        <property name="suffix" value=".jsp"/>
    </bean>-->
    
</beans>

4.配置前端控制器(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">

    <!-- 配置SpringMVC前端控制器 -->
    <servlet>
        <servlet-name>mySpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定SpringMVC配置文件 -->
        <!-- SpringMVC的配置文件的默认路径是/WEB-INF/${servlet-name}-servlet.xml -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <!--在下面的配置中写上大于0的数字后,当服务器启动的时候就会加载上面的初始化类,就不会让用户在点击的时候再做初始化
        从而减少用户的等待时间-->
        <load-on-startup>1</load-on-startup>


    </servlet>
    <servlet-mapping>
        <servlet-name>mySpringMVC</servlet-name>
        <!-- 设置所有以action结尾的请求进入SpringMVC -->
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>


</web-app>

5.创建控制器

package com.helong.springmvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/*使用注解时,要在springmvc.xml中开启注解扫描 并在设置中设置开启注解*/
@Controller
public class MyController {

    //匹配请求可以省略action
    /*如果有数据要发送到其他界面就可以返回ModeAndView来传递数据*/
    /*@RequestMapping("/first.action") 这个位置的.action可以省略*/
    @RequestMapping("/first")
    public ModelAndView show1(){

        ModelAndView modelAndView = new ModelAndView();

        //请求过来后跳转到某一个界面 往另一个界面中传一些数据(存放数据)
        modelAndView.addObject("name","helong");
        //访问该页面的时候会自动跳转到下面的页面,并且可以在这个页面中获取上方存入的数据
       /* modelAndView.setViewName("/result.jsp");*/
        //重定向到其他的action(也就是直接执行其他action,这个位置.action不能省略)
        modelAndView.setViewName("redirect:/second.action");//发送请求的时候不能省略.action

        return modelAndView;
    }


    /*如果不需要传递数据到其他界面就可以直接返回一个String转发到对应的界面*/
    /*@RequestMapping("/second.action") 这个位置的.action可以省略*/
    @RequestMapping("/second")
    public String show2(){
        ModelAndView modelAndView = new ModelAndView();
        /*转发到return后面设置的界面*/
      /*  return "/result.jsp";*/
        //重定向到其他页面(直接跳转到其他页面)
        /*重定向是把前端发送过来的数据直接转发给其他定向的位置
        * 重定向还会改变浏览器的地址栏信息*/
        return "redirect:/result.jsp";
    }
}

6.创建发送请求jsp文件,并设置请求

 <a href="${pageContext.request.contextPath}/first.action">发送请求到result.jsp界面获取数据</a>
  <a href="${pageContext.request.contextPath}/second.action">发送请求到result.jsp界面获取数据</a>

7.创建接收数据/转发条跳转的jsp界面

<!--直接可以通过在控制器中添加的数据模型名称来获取所设置的数据-->
<h1>result--界面</h1>
<h2>name:${name}</h2>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章