1.准备所需jar包:
2.创建动态web项目:
3.项目取名为AmazingWorld,点击next:
4.把web.xml勾选上:
5.项目结构如下:
+
注意application.xml位置,它在src根目录下。
6.源代码如下:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>AmazingWorld</display-name>
<!-- 配置请求分发器 -->
<servlet>
<servlet-name>AmazingWorld</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 指定application.xml文件 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</init-param>
<!-- 启动优先级,优先加载Spring的xml配置文件,数字越小,优先级越大 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AmazingWorld</servlet-name>
<url-pattern>/</url-pattern> <!-- 拦截所有的请求 -->
</servlet-mapping>
</web-app>
application.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
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/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置bean -->
<bean class="com.edu.springmvc.AmazingWorldController"/>
</beans>
AmazingWorldController.java
package com.edu.springmvc;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller // 指定所需要映射的类
public class AmazingWorldController {
@RequestMapping("/amazing") //前端传递来的请求用哪个方法处理
public ModelAndView model(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView(); //创建ModelAndView对象
mv.addObject("amazing", "Amazing World"); //设置参数
mv.setViewName("/amazing.jsp"); //指明指定响应的jsp页面
return mv; //返回ModelAndView对象
}
}
amazing.jsp
<%@ 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>Amazing World</title>
</head>
<body>
${amazing } <!-- EL表达式,获取amazing值 -->
</body>
</html>
7.打开浏览器,输入地址如下:
创建成功!