SpringMVC最简单Demo

本文介绍了SpringMVC的基本工作流程,并通过一个简单的Demo详细解释了各组件的作用及配置方式,包括web.xml、spring-servlet.xml等关键文件的设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里提供一个最简单的Demo,代码部分参考了 lanxinlll的下载资源,欢迎大家前去下载其文件:http://download.youkuaiyun.com/detail/lanxinlll/2036930


我掌握的很基础,简单说一下我理解的 SpringMVC 流程,同时交代这个Demo的代码给大家:


1.提交请求,例如query.do或者hello.spring,具体后缀在著名的web.xml中配置,配置如下:



<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	
	<!-- 配置 dispatchAction -->
	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>*.spring</url-pattern>
	</servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

2.提交了请求后,根据web.xml提供的信息,交予Sping处理,于是我们需要Spring配置文件,同样在WEB-INF文件夹下,取名为spring-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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

	<bean name="/hello.spring" class="com.spring.mvc.test.HelloController">
		<property name="jsp" value="/hello.jsp" />
	</bean>

</beans>

3.确实很简单,那么根据文件我们可以看到,我们需要一个java类,包位置是“com.spring.mvc.test“,类名是HelloController,还有个叫做jsp的参数,HelloController内容如下:


package com.spring.mvc.test;

import java.io.IOException;

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

import org.apache.struts.tiles.ComponentContext;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 * Spring MVC  Demo
 * @author lanxin
 * @from   mikegood
 * @date   2010-01-31
 */
public class HelloController implements Controller,
		org.apache.struts.tiles.Controller {
    
	String jsp = "";
    //注入  要转到的jsp 名称
	public void setJsp(String jsp) {
		this.jsp = jsp;
	}

	@Override
	public ModelAndView handleRequest(
			HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String txtWord  = request.getParameter("txtWord");
		//相当于
		/**
		 * request.setAttribute("txtWord", txtWord);
		 * request.getRequestDispatcher(jsp).forward(request, response);
		 */
		return new ModelAndView(jsp,"txtWord",txtWord);
	}

	@Override
	public void execute(ComponentContext arg0, HttpServletRequest arg1,
			HttpServletResponse arg2, ServletContext arg3) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void perform(ComponentContext arg0, HttpServletRequest arg1,
			HttpServletResponse arg2, ServletContext arg3)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}
}


4.而我们还需要一个名为hello.jsp的文件,作为处理返回后的内容:


<%@ page contentType="text/html; charset=GBK"%>
<html>
	<head>
		<title>SPRING MVC RESULT</title>
	</head>
	<body>
		 <h2>The word is :${txtWord }</h2>
	</body>
</html>


5.最后,我们需要index.jsp作为项目入口,用于提交请求:


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Spring MVC Demo</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>
  <form action="hello.spring">
    <h2>Spring MVC  DEMO</h2>
    <div>
    	Please  Enter a  word <input type="text" name="txtWord" />
    	<br/>
    	  <input type="submit">
    </div>
    </form>
  </body>
</html>


最后,附图如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值