【SpringMVC】入门案例
2017-11-02 17:04
429 查看
1、配置前端控制器:DispatcherServlet(在web.xml里面)
3、Controller
5、效果
转自:http://blog.csdn.net/lwt976647637/article/details/73741773
<span style="font-size:14px;"><span style="color:#17365d;"> </span> <!-- 定义Spring MVC的前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 让Spring MVC的前端控制器拦截所有请求 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></span><span style="color:#17365d;"><span style="font-size: 16pt;"> </span> </span>2、springmvc-config.xml配置文件
<span style="font-size:14px;"><?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- spring可以自动去扫描base-pack下面的包或者子包下面的java文件, 如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean --> <context:component-scan base-package="com.lwt.controller"/> <!-- 配置处理映射器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <!-- 配置处理器适配器--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/> </beans> </span>
3、Controller
<span style="font-size:14px;">@Controller public class HelloController{ private static final Log logger = LogFactory .getLog(HelloController.class); /** * org.springframework.web.bind.annotation.RequestMapping注解 * 用来映射请求的的URL和请求的方法等。 * hello只是一个普通方法。 * 该方法返回一个包含视图路径或视图路径和模型的ModelAndView对象。 * */ @RequestMapping(value="/hello") public ModelAndView hello(){ logger.info("hello方法 被调用"); // 创建准备返回的ModelAndView对象,该对象通常包含了返回视图的路径、模型的名称以及模型对象 ModelAndView mv = new ModelAndView(); //添加模型数据 可以是任意的POJO对象 mv.addObject("message", "Hello World!"); // 设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面 mv.setViewName("/WEB-INF/content/welcome.jsp"); // 返回ModelAndView对象。 return mv; } }</span>4、页面
<span style="font-size:14px;"><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>welcome</title> </head> <body> <!-- 页面可以访问Controller传递传递出来的message --> ${requestScope.message} </body> </html></span>
5、效果
转自:http://blog.csdn.net/lwt976647637/article/details/73741773
相关文章推荐
- SpringMVC的第一个入门案例
- Springmvc入门案例(1)
- 【SpringMVC】入门案例
- SpringMVC的第一个入门案例
- 10SpringMvc_springmvc快速入门小案例(注解版本)
- SpringMvc的入门案例
- SpringMVC入门案例
- SpringMVC的第一个入门案例
- SpringMvc之入门案例-yellowcong
- SpringMVC入门案例及请求流程图(关于处理器或视图解析器或处理器映射器等的初步配置)
- springMVC学习笔记之入门案例
- SpringMVC学习(1):SpringMVC框架结构详解和入门案例详解
- springmvc入门案例可能出现的错误
- SpringMVC--入门案例
- 02SpringMvc_springmvc快速入门小案例(XML版本)
- SpringMVC入门案例及请求流程图(关于处理器或视图解析器或处理器映射器等的初步配置)
- Dubbox --- springmvc结合入门案例
- SpringMVC入门案例——注解配置方式
- Spring 入门案例(含IOC、AOP、SpringMVC、Spring JDBC)
- SpringMVC入门案例