[架构]myeclipse配置SpringMVC 以及简单应用 教程
1.Spring的安装
- 方式1:参考网址:https://maven.springframework.org/release/org/springframework/spring/ 下载后手动导入jar包
- 方式2:在myeclipse自动安装,新建一个web项目,右击项目,在弹出的页面中点击如下操作
等待安装完成。其中myeclipse会自动导入所需的jar包
2.SpringMVC的配置
在WEB-INF目录下创建web.xml文件
web.xml内容如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="3.0" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 7 <!-- 配置SpringMVC --> 8 <servlet> 9 <servlet-name>dispatcher</servlet-name> 10 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 11 <init-param> 12 <param-name>contextConfigLocation</param-name> 13 <param-value>classpath:dispatcher-servlet.xml</param-value> 14 </init-param> 15 </servlet> 16 17 <servlet-mapping> 18 <servlet-name>dispatcher</servlet-name> 19 <!-- 监听所有请求 --> 20 <url-pattern>hello</url-pattern> 21 </servlet-mapping> 22 </web-app>
注:其中
<servlet-mapping> <servlet-name>dispatcher</servlet-name> <!-- 监听所有请求 --> <url-pattern>hello</url-pattern> </servlet-mapping>
<url-pattern>/</url-pattern> 相当于一个小区的门卫大爷,会拦截所有hello的请求
继而门卫大爷会直接给请求进入的人根据 <servlet-name>dispatcher</servlet-name> 中的dispatcher找 <servlet> 中的 <servlet-name>dispatcher</servlet-name>
找到如下部分代码:
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:dispatcher-servlet.xml</param-value> </init-param> </servlet>
dispatcher中指明 <param-value>classpath:dispatcher-servlet.xml</param-value> 所以接下来创建dispatcher-servlet.xml
如下:
内容如下:
<?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-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 定义要扫描 controller的包 --> <context:component-scan base-package="com.frank.springmvc.controller" /> <mvc:default-servlet-handler /> <!-- 启动注解驱动 SpringMVC 功能 --> <mvc:annotation-driven /> <!-- 配置视图解析器解析路径 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!-- 定义视图存放路径 --> <property name="prefix" value="/WEB-INF/" /> <!-- 定义视图后缀 --> <property name="suffix" value=".jsp" /> </bean> </beans>
<context:component-scan base-package="com.frank.springmvc.controller" /> 是你要创建的controller的包名,可自行更改但必须保持一致。
<!-- 配置视图解析器解析路径 -->中的
<property name="prefix" value="/WEB-INF/" /> 为前缀
<property name="suffix" value=".jsp" /> 为后缀
在下面会详细解释
3.SpringMVC的简单应用
接下来创建package
包名如上
com.frank.springmvc.controller
在该包中创建HellpSpringMVC类代码如下:
1 package com.frank.springmvc.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 @Controller 7 public class HellpSpringMVC { 8 9 @RequestMapping("hello") 10 public String hello() { 11 return "hello"; 12 } 13 }
@RequestMapping("hello") 这就是请求的hello,
在WEB-INF中创建hello.jsp
代码如下:
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'hello.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 Spring Hello - Welcome<br> 27 </body> 28 </html>
更改index.jsp的代码:
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'index.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 23 <body> 24 <a href="hello">Hello</a> 25 </body> 26 </html>
其中 <a href="hello">Spring Hello</a> 请求为hello会被web.xml的setvlet-mapping拦截
根据Controller类 HellpSpringMVC 的 @RequestMapping("hello") 执行 public String hello() 返回hello字符串
根据dispatcher-servlet.xml视图解析器(详见:上面的前缀以及后缀)
拼接成 /WEB-INF/ hello .jsp 这就是返回路径
结果如下:
点击hello进入