SpringMVC第一天HelloWorld

1,普通Servlet的流程是通过配置和来拦截请求交给对应的Servlet来处理

使用SpringMVC需要配置一个SpringMVC自带的Servlet,**DispatcherServlet,**使用他来拦截请求交给SpringMVC处理  web.xml中配置

springDispatcherServlet

org.springframework.web.servlet.DispatcherServlet

设置springmvc核心配置文件路径,如果不设置,默认到web-inf下去找名字为springDispatcherServlet-servlet.xml的文件

    
contextConfigLocation
classpath:springmvc.xml

设置为启动时加载

1

  springDispatcherServlet

  /

  注意这里使用 / 在struts2中使用的是 /*  表示拦截所有请求,

  /login 代表拦截以/login开头的请求   .do只拦截以.do为后缀的请求

  如果想要同时使用SpringMVC和Servlet就不能使用 / 符号,也就是不能让springmvc拦截所有请求

2,

创建springmvc.xml

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

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

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-4.0.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

_http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"_\>

<context:component-scan base-package=_"com.lyf.springmvc.controller"_ />

<property ****name="suffix" ****value=".jsp"********>

3,创建java类 添加@Controller注解   在方法上添加@RequetMapping注解里面指定请求的url

如@RequetMapping("/login")****

方法返回值自己设置 如return success;

@Controller

public class HelloWorld{

@RequetMapping("/login")

public String test(){

return "success";

}

}

index.jsp页面假如有一个 To  HelloWorld

success.jsp中内容为First SpringMVC

当点击To HelloWorld就会转到HelloWorld中的test方法处理 最后return success  通过视图解析器 ,最后跳转到view文件夹下的success.j****sp

评论

暂无评论