您的当前位置:首页正文

springmvc注解基本入门

2020-08-07 来源:步旅网
springmvc注解基本⼊门

简单介绍使⽤springmvc注解的基本流程.1.在web.xml中配置DispatcherServlet

xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd\" id=\"MyWebApp\" version=\"2.5\">

springMVC

index.jsp

springmvc

org.springframework.web.servlet.DispatcherServlet 1

springmvc *.do

2.创建核⼼配置⽂件springmvc-servlet.xml(WEB-INF/...)

在给配置⽂件中,主要配置注解驱动,配置包扫描就不在配置Bean了(扫描注解),配置视图解析器

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.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.xsd\">

3.定义视图(这⾥创建⼀个hello.jsp⽂件,符合上⾯的视图规则,所以要放到WEB-INF/views/)

<%@ page language=\"java\" contentType=\"text/html; charset=UTF-8\" pageEncoding=\"UTF-8\"%>

Insert title here

${msg }

4.配置handler

@RequestMapping(\"test\")@Controller

public class LoghinController {

@RequestMapping(\"/show1\") public ModelAndView show1(){

ModelAndView mv = new ModelAndView(\"hello\"); mv.addObject(\"msg\第⼀个springmvc程序1\"); return mv; }}}

5.在浏览器输⼊指定的地址,运⾏完成

WEB-INF/{servletName}-servlet.xml

因篇幅问题不能全部显示,请点此查看更多更全内容