写点什么

Spring MVC—XML 配置与注解配置 + 使用注解完成请求参数绑定

用户头像
极客good
关注
发布于: 刚刚

<servlet-mapping>


<servlet-name>springmvc</servlet-name>


<url-pattern>/</url-pattern>


</servlet-mapping>


</web-app>


要想把 SpringMVC 框架应用到 Web 项目中,我们首先需要在 web.xml 添加一个 Servlet —— DispatchcerServlet。DispatcherServlet 是 SpringMVC 的集中访问点,其核心功能就是分发请求,而且能与 Spring IoC 容器无缝集成,从而可以获得 Spring 的所有好处。


2,在 web.xml 中指定路径配置 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">


<bean name="/hello" class="cn.edu.tju.rico.controller.He


【一线大厂Java面试题解析+核心总结学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


lloController" />


<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />


<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />


</beans>


3,实现 SpringMVC 配置文件中配置的 Controller


public class HelloController implements Controller {


public ModelAndView handleRequest( HttpServletRequest request,


HttpServletResponse response ) throws Exception


{


//创建准备返回的 ModelAndView 对象,如名所示,该对象通常包含了返回视图名、模型名称以及模型对象


ModelAndView mv = new ModelAndView();


// 添加模型数据,可以是任意的 POJO 对象


mv.addObject( "message", "Hello, Rico..." );


// 设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面


mv.setViewName( "/WEB-INF/views/welcome.jsp" );


// 返回 ModelAndView 对象


return(mv);


}


}


4,相应的视图页面


<?xml version="1.0"?>


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%


String path = request.getContextPath();


String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";


%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


<html>


<head>


<base href="<%=basePath%>">


<title>welcome</title>


<meta http-equiv="pragma" content="no-cache">


<meta http-equiv="cache-control" content="no-cache">


<meta http-equiv="expires" content="0">


<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">


<meta http-equiv="description" content="This is my page">


<body>${requestScope.message}


<br></br></body>


</meta>


</meta>


</meta>


</meta>


</meta>


</base>


</head>


</html>


SpringMVC 应用开发流程 DEMO:Annotation 配置


================================================================================================


1,在 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_2_5.xsd" id="WebApp_ID" version="2.5">


<display-name>SpringMVCDemo</display-name>


<servlet>


<servlet-name>springmvc</servlet-name>


<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>


<init-param>


<param-name>contextConfigLocation</param-name>


<param-value>classpath:springmvc.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,在 web.xml 中指定路径配置 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


<context:component-scan base-package="cn.edu.tju.rico"></context:component-scan>


<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />


用户头像

极客good

关注

还未添加个人签名 2021.03.18 加入

还未添加个人简介

评论

发布
暂无评论
Spring MVC—XML配置与注解配置+使用注解完成请求参数绑定