springmvc入门简例

本文详细介绍了一个Spring MVC项目的具体配置过程,包括web.xml配置、springmvc-servlet.xml配置、控制器TestController实现、TestUtil组件使用及视图test.jsp展示。通过这些配置实现了从前端到后端的完整交互流程。

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

1、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>springmvc</display-name>
  
  
  <!-- Spring-MVC的请求转发器 -->
  <servlet>
	  <servlet-name>spring-mvc</servlet-name>
	  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	  <init-param>
		  <param-name>contextConfigLocation</param-name>
		  <param-value>/WEB-INF/springmvc-servlet.xml</param-value>
	  </init-param>
	  <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>spring-mvc</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  
  <!-- Spring配置 -->   
  <listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
  </listener> 
  <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->  
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:config/applicationContext.xml</param-value>
  </context-param>

  
  <!-- 编码过滤器 -->
  <filter>
  	<filter-name>encodingFilter</filter-name>
  	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  	<init-param>
    	<param-name>encoding</param-name>
    	<param-value>UTF-8</param-value>
  	</init-param>
  	<init-param>
    	<param-name>forceEncoding</param-name>
    	<param-value>true</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>encodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping> 


  
  
  
  <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>
</web-app>

2、springmvc-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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc
	   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
	   http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!--  
	当映射为@RequestMapping("/user/add")时:
	1、拦截*.do,例如:/user/add.do,弊端:所有的url都要以.do结尾。不会影响访问静态文件。
	2、拦截/app/*,例如:/app/user/add,弊端:请求的url都要包含/app,@RequestMapping("/user/add")中不须要包含/app。
	3、拦截/,例如:/user/add,弊端:对jpg,js,css静态文件的访问也被拦截不能正常显示。后面有解决办法。
	4、拦截/*,可以走到Action中,但转发到jsp时再次被拦截,不能访问到jsp。
-->

	<!-- 扫描web包,应用Spring的注解 -->
	<context:component-scan base-package="com.springmvc.test"/>
	
	<!-- 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,
	是spring MVC为@Controllers分发请求所必须的。 -->
    <mvc:annotation-driven /> 

   
	<!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:viewClass="org.springframework.web.servlet.view.JstlView" 
		p:prefix="/WEB-INF/jsp/"
		p:suffix=".jsp" />

     <!-- 对静态资源文件的访问  方案一 (二选一)  
    <mvc:default-servlet-handler/>  
     -->
       
    <!-- 对静态资源文件的访问  方案二 (二选一) -->
	<mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/>  
	<mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>  
	<mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/>   
     
</beans>

3、TestController

package com.springmvc.test.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.springmvc.test.util.TestUtil;

/**
 * 
 * @author xuzebin
 *
 */
@Controller
@RequestMapping("/test")
public class TestController {
	@Autowired
	private TestUtil testUtil;
	
	@RequestMapping("/show")
	public String show(){
		System.out.println(testUtil.say());
		return "test";
	}

}
4、TestUtil

package com.springmvc.test.util;

import org.springframework.stereotype.Component;

/**
 * 
 * @author xuzebin
 *
 */
@Component("testUtil")
public class TestUtil {
	public String say(){
		return "hello world";
	}
}

5、test.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 'test.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>
    This is my test JSP page. <br>
  </body>
</html>
6、例子的整体目录

7、所需jar

免积分下载地址


8、基本运行原理

    1. 客户端请求提交到DispatcherServlet
    2. 由DispatcherServlet控制器查询一个或多个HandlerMapping,找到处理请求的Controller
    3. DispatcherServlet将请求提交到Controller
    4. Controller调用业务逻辑处理后,返回ModelAndView
    5. DispatcherServlet查询一个或多个ViewResoler视图解析器,找到ModelAndView指定的视图
    6. 视图负责将结果显示到客户端


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值