代码如下:
1.SpringMVC的 web.xml文件的编写:(注册dispatcherServlet)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <!--1.dispatcherServlet--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--Springmvc配置文件--> <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> </web-app>
2.SpringMVC配置文件的编写:(注解自动扫描,静态资源的过滤,注解驱动,视图解析器)
<?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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--包下注解的自动扫描 @Controller--> <context:component-scan base-package="com.xbf.controller"/> <!--静态资源过滤 :springmvc不处理静态资源--> <mvc:default-servlet-handler/> <!--注解驱动 @RequestMapping("****")--> <mvc:annotation-driven/> <!--视图解析器 就是一个bean--> <!--/WEB-INF/jsp/ 该路径下的资源比较安全,用户不能直接访问--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
3.Controller层的编写:(职能:进行数据模型的处理,并返回视图和数据模型)
package com.xbf.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Controller public class ModelTest { //1.使用 ModelAndView对象 @RequestMapping("/t1") public ModelAndView test1(){ ModelAndView mv = new ModelAndView(); mv.addObject("msg","modelandview is nice"); mv.setViewName("test"); return mv; } //2.使用servletAPI //请求转发 request 可携带参数 //页面重定向 response 不能携带参数 @RequestMapping("/t2") public void test2(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { request.setAttribute("msg","servletAPI is nice"); //请求转发进行页面的跳转 request.getRequestDispatcher("index.jsp").forward(request,response); } //3.使用springmvc 但不用视图解析器 @RequestMapping("/t3") public String test3(HttpServletRequest request){ request.setAttribute("msg","Controller Test3"); return "redirect:index.jsp"; //重定向不能携带参数值 } @RequestMapping("/t33") public String test33(HttpServletRequest request){ request.setAttribute("msg","Controller Test3"); return "forward:index.jsp"; //请求转发可以携带参数 } //4. 使用springmvc 并且使用视图解析器 //使用Model 对象可以放参数值 @RequestMapping("/t4") public String test4(){ return "test"; } @RequestMapping("/t44") public String test44(Model model){ model.addAttribute("msg","Springmvc use 视图解析器"); return "test"; } }
4.前端页面:(获取到controller层的数据模型并进行渲染后,再返回)
尴尬也没怎么渲染只是将数据拿出来看看 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>servletAPI</title> </head> <body> ${msg} </body> </html>
总结点:
通过ModleAndView对象可以将页面跳转换到 web-inf目录下;
请求转发(request)和页面重定向(response)不能将页面跳转到web-inf目录下,会报 404错误!
并且重定向(response)不能携带参数值;
lk