zoukankan      html  css  js  c++  java
  • Spring MVC(一)

    一、示例

    1.首先,导入SpringMVC需要的jar包。

    2.添加Web.xml配置文件中关于SpringMVC的配置
    复制代码
      <!--configure the setting of springmvcDispatcherServlet and configure the mapping-->
      <servlet>
          <servlet-name>springmvc</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc-servlet.xml</param-value>
            </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下添加springmvc-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"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        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-4.1.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">                    
    
        <!-- scan the package and the sub package -->
        <context:component-scan base-package="test.SpringMVC"/>
    
        <!-- don't handle the static resource -->
        <mvc:default-servlet-handler />
    
        <!-- if you use annotation you must configure following setting -->
        <mvc:annotation-driven />
        
        <!-- configure the InternalResourceViewResolver -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
                id="internalResourceViewResolver">
            <!-- 前缀 -->
            <property name="prefix" value="/WEB-INF/jsp/" />
            <!-- 后缀 -->
            <property name="suffix" value=".jsp" />
        </bean>
    </beans>
    复制代码

    4.在WEB-INF文件夹下创建名为jsp的文件夹,用来存放jsp视图。创建一个hello.jsp,在body中添加“Hello World”。

    5.建立包及Controller,如下所示

    6.编写Controller代码

    复制代码
    @Controller
    @RequestMapping("/mvc")
    public class mvcController {
    
        @RequestMapping("/hello")
        public String hello(){        
            return "hello";
        }
    }
    复制代码

    7.启动服务器,键入 http://localhost:8080/项目名/mvc/hello

    二,配置解析

    1,DispatcherServlet

    是前置控制器,配置在web.xml中。拦截匹配的请求,Servelt拦截匹配规则要自己定义,把拦截下来的请求,依据相应的规则分发到目标Controller来处理,是Spring MVC的第一步。

    2,InternalResourceViewResolver

    视图名称解析器

    3,注解

    @Controller 负责注册一个 controller bean到spring上下文

    @RequestMapping 为控制器指定可以处理哪些URL请求

    @ResponseBody 用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区

    @RequestParam 在处理方法入参处使用@RequestParam可以把请求参数传递给请求方法

    四、各种参数绑定方式

    参考http://www.cnblogs.com/IvySue/p/7496497.html

    五、在Controller中使用redirect方法处理请求

     //redirect 
        @RequestMapping("/redirect")
        public String redirect(){
            return "redirect:hello";
        }
  • 相关阅读:
    Ubuntu中Nginx的安装与配置
    在Ubuntu中安装Redis
    微博开放平台
    QQ互联 回调地址
    PostgreSQL在Ubuntu上安装指南
    postgresql常用命令
    在Linux下查看环境变量
    vue-04-组件
    vue-03-style与class
    vue-02-安装-指令
  • 原文地址:https://www.cnblogs.com/IvySue/p/7496190.html
Copyright © 2011-2022 走看看