标签:ble intern alt char 过程 oca base inter rri
SpringMVC最核心:DispatcherServlet
SpringMVC环境搭建:
结构:
过程:
1.导包
2.声明SpringMVC核心Servlet:org.springframework.web.servlet.DispatcherServlet
声明Spring配置文件的路径:
1.可以自己声明
2.默认值:/WEB-INF/springDispatcherServlet-servlet.xml
3.新建Spring配置文件
(注解方式)
1.扫描器:<context:component-scan base-package="com.maya"></context:component-scan>
2.配置视图解析器,设置前缀后缀
3.开启SpringMVC注解驱动:<mvc:annotation-driven></mvc:annotation-driven>
4.编写控制器类,声明方法
5.运行页面
代码:
web.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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"> 3 <display-name>test_easyui2</display-name> 4 5 <!-- The front controller of this Spring Web application, responsible for handling all application requests --> 6 <servlet> 7 <servlet-name>springDispatcherServlet</servlet-name> 8 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 9 <!-- 初始化参数,如果不写有默认值,/WEB-INF/springDispatcherServlet-servlet.xml --> 10 <init-param> 11 <param-name>contextConfigLocation</param-name> 12 <param-value>classpath:spring-mvc.xml</param-value> 13 </init-param> 14 <!-- 值大于0时,启动服务器时加载,数字越小,优先级越高 --> 15 <load-on-startup>1</load-on-startup> 16 </servlet> 17 18 <!-- Map all requests to the DispatcherServlet for handling --> 19 <servlet-mapping> 20 <servlet-name>springDispatcherServlet</servlet-name> 21 <url-pattern>/</url-pattern> 22 </servlet-mapping> 23 </web-app>
spring-mvc.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 9 10 <!-- <bean id="/helloworld" class="com.maya.controller.HelloWorldController"></bean> 11 12 spring2.5之前的配置方式 13 将SpringBean的名字作为映射路径的请求 14 <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> 15 处理适配器,将请求映射给conntroller类 16 <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean> 17 视图解析器 18 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean> 19 --> 20 21 <!-- 注解方式 --> 22 <!-- 扫描器 --> 23 <context:component-scan base-package="com.maya"></context:component-scan> 24 <!-- 视图解析器 --> 25 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 26 <property name="prefix" value="/page/"></property> 27 <property name="suffix" value=".jsp"></property> 28 </bean> 29 <!-- 开启springmvc注解驱动 --> 30 <mvc:annotation-driven></mvc:annotation-driven> 31 32 </beans>
两种处理方式类:
1 package com.maya.controller; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.web.servlet.ModelAndView; 7 import org.springframework.web.servlet.mvc.Controller; 8 9 //只能处理单一请求 10 public class HelloWorldController implements Controller { 11 12 @Override 13 public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { 14 ModelAndView mav=new ModelAndView(); 15 mav.setViewName("page/success.jsp"); 16 17 mav.addObject("message","测试,属性message"); 18 //以上方法等同于request.setAttribute(arg0, arg1); 19 return mav; 20 } 21 22 }
注解方式:
1 package com.maya.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.servlet.ModelAndView; 6 7 @Controller 8 public class TestController { 9 10 @RequestMapping("/testHelloWorld") 11 public ModelAndView testHelloWorld(){ 12 ModelAndView mav=new ModelAndView(); 13 mav.addObject("msg", "测试属性msg"); 14 mav.setViewName("success"); 15 return mav; 16 } 17 }
jsp页面:
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <div> 11 <span>spring 2.5 之前</span> 12 <a href="helloworld">传送门</a> 13 </div> 14 <div> 15 <span>基于注解</span> 16 <a href="testHelloWorld">传送门2</a> 17 </div> 18 </body> 19 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <h1>成功!!!</h1> 11 ${message } 12 ${msg } 13 </body> 14 </html>
常用注解:
@Controller 声明控制器类
@RequestMapping 声明映射的请求
@RequestParam 参数绑定
@PathVariable 绑定路径参数
@ResponseBody 将方法的返回值直接返回
@SessionAttribute 将指定的对象放到session
@ModelAttribute
标签:ble intern alt char 过程 oca base inter rri
原文地址:http://www.cnblogs.com/jiangwz/p/7611533.html