搭建 第一个SpringMvc 项目

本文详细介绍了如何在Web项目中集成Spring MVC框架,并通过一个简单的案例演示了其核心组件和工作流程。从加入相关JAR包开始,到编写WEB.xml配置文件、实现Java代码逻辑,再到展示JSP页面的交互效果,全程手把手教学。

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

1.加入相关JAR

commons-logging-1.1.1.jar

spring-aop-4.1.4.RELEASE.jar

spring-beans-4.1.4.RELEASE.jar

spring-context-4.1.4.RELEASE.jar

spring-core-4.1.4.RELEASE.jar

spring-expression-4.1.4.RELEASE.jar

spring-tx-4.1.4.RELEASE.jar

spring-web-4.1.4.RELEASE.jar

spring-webmvc-4.1.4.RELEASE.jar


2.编写WEB.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	 <servlet>
	 	<servlet-name>Springmvc</servlet-name>
	 	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	 	 <!-- 配置DispatcherServlet 一个初始化参数;配置springmvc 的配置文件的位置和名称 -->
	 	 <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,
	 	 名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml-->
	 	<load-on-startup>1</load-on-startup>
	 </servlet>
	 <servlet-mapping>
	 	<servlet-name>Springmvc</servlet-name>
	 	<url-pattern>/</url-pattern>
	 </servlet-mapping>
</web-app>
SpringMvc 配置文件的文件名 为 web.xml中的[servlet-name]-servlet.xml
<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:p="http://www.springframework.org/schema/p"  
 xmlns:mvc="http://www.springframework.org/schema/mvc"  
 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-3.0.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-3.0.xsd">  
     <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->  
     <mvc:annotation-driven />  
     <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->  
     <context:component-scan base-package="com.sqc.springmvc" />  
     <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->  
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     	 <property name="prefix" value="/WEB-INF/views/"></property>
     	 <property name="suffix" value=".jsp"></property>
     </bean>  
</beans>  
4.java代码
package com.sqc.springmvc.handlers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWord {
	private final static String SUCCESS="success";
	/**
	 * 1. 使用 @RequestMapping 注解来映射请求的 URL
	 * 2. 返回值会通过视图解析器解析为实际的物理视图, 对于 InternalResourceViewResolver 视图解析器, 会做如下的解析:
	 * 通过 prefix + returnVal + 后缀 这样的方式得到实际的物理视图, 然会做转发操作
	 * 
	 * /WEB-INF/views/success.jsp
	 * 
	 * @return
	 */
	@RequestMapping("/helloword")
	public String helloWord(){
		System.out.println("hello Word");
		return SUCCESS;
	}
}



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 'success.jsp' starting page</title>
  </head>
  <body>
   success page. <br>
  </body>
</html>

访问 路径如下 http://127.0.0.1:8080/SpringMvc/helloword

页面展示展示出来 即可





                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值