SpringMVC-2 第一个SpringMVC程序

目录

SpringMVC请求流程

创建 Web 应用并引入 JAR 包

Spring MVC 配置:在 web.xml 中配置 Servlet,创建 Spring MVC 的配置文件

创建 Controller(处理请求的控制器)

创建 View

部署运行

常见错误

总结


​​​​​​​

SpringMVC请求流程

创建 Web 应用并引入 JAR 包

在MyEclipse中创建一个新的web工程,注意勾选生成web.xml。或者手动创建也可以。

         右键项目名,选择BuildPath,导入需要的jar包,环境搭建完毕。

Spring MVC 配置:在 web.xml 中配置 Servlet,创建 Spring MVC 的配置文件

        Spring MVC 是基于 Servlet 的,DispatcherServlet 是整个 Spring MVC 框架的核心,所有请求都经过DispatcherServlet递给Spring框架,所以我们首先配置DispatcherServlet。打开web.xml,更改如下代码。

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>mvc-1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  		<servlet-name>dispatcherServlet</servlet-name>
  		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  		<!-- 初始化参数,配置Spring MVC配置文件的位置以及名称 -->
  		<init-param>
  			<param-name>contextConfigLocation</param-name>
  			<param-value>classpath:springmvc.xml</param-value>
  		</init-param>
  		<!-- 表示容器启动时,立即加载dispatcherServlet -->
  		<load-on-startup>1</load-on-startup>  		
  </servlet>
    <!-- Spring MVC的前端控制器拦截所有的请求 -->
  <servlet-mapping>
  		<servlet-name>dispatcherServlet</servlet-name>
  		<url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

        可以看到,我们指定了一个名为springmvc.xml的配置文件,所以下面我们创建该文件。在src目录下创建springmvc.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	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.xsd
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	<!-- 配置处理器Handle,映射为“/hello”的请求 -->
	<bean name="/hello" class="controller.HelloController" />
	<!-- 配置视图解析器,将控制器方法返回的逻辑视图解析为物理视图 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />

</beans>

创建 Controller(处理请求的控制器)

        在src目录下创建controller包,在包内创建HelloController.java

package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloController implements Controller{

	@Override
	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		// TODO Auto-generated method stub
		ModelAndView mav = new ModelAndView();
		mav.addObject("msg","hello");
		mav.setViewName("result.jsp");
		return mav;
	}

}

创建 View

        在WebRoot中创建result.jsp和index.jsp。

result.jsp:

<%@ 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>My JSP 'index.jsp' starting page</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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    ${msg}<br>
  </body>
</html>

index.jsp:

<%@ 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>My JSP 'index.jsp' starting page</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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <a href="hello">ClickMe.</a>
    <br>
  </body>
</html>

部署运行

        将项目部署到tomcat上,查看运行效果,点击ClickMe,跳转至reslut界面。

常见错误

        

 jdk版本过低,将jdk切换到8以上即可。

如果点击后404则可能是配置文件中映射的类名和controller名不一样。

总结

1. 用户访问 /hello
2. 根据web.xml中的配置 所有的访问都会经过DispatcherServlet
3. 根据 根据配置文件springmvc-servlet.xml ,访问路径 /hello
会进入 helloController类
4. 在 helloController中指定跳转到页面result.jsp,并传递message数据
5. 在result.jsp中显示message信息

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值