SpringMVC注解开发

本文详细介绍如何从零开始搭建一个基于Spring MVC的Web项目,包括创建项目、配置web.xml、设置视图解析器、编写控制器等步骤。

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

一、新建一个web项目


二、导入相关jar包

包括Spring所有包+commons-logging-1.1.1.jar

三、编写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>MyBatisWeb</display-name>
  <servlet>
  <span style="white-space:pre">	</span><servlet-name>SpringServlet</servlet-name>
  <span style="white-space:pre">	</span><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <span style="white-space:pre">	</span><init-param>
  <span style="white-space:pre">		</span><param-name>contextConfigLocation</param-name>
  <span style="white-space:pre">		</span><param-value>classpath:mvc.xml</param-value>
  <span style="white-space:pre">	</span></init-param>
  <span style="white-space:pre">	</span><load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <span style="white-space:pre">	</span><servlet-name>SpringServlet</servlet-name>
  <span style="white-space:pre">	</span><url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>hello.do</welcome-file>
  </welcome-file-list>
</web-app>

四、在src目录下新建一个SpringServlet-servlet.xml的配置文件(命名方式为web.xml中定义的DispatcherServlet名-servlet.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
<span style="white-space:pre">	</span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
<span style="white-space:pre">	</span>xmlns:context="http://www.springframework.org/schema/context"
<span style="white-space:pre">	</span>xsi:schemaLocation="
<span style="white-space:pre">	</span>http://www.springframework.org/schema/beans
<span style="white-space:pre">	</span>http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
<span style="white-space:pre">	</span>http://www.springframework.org/schema/context
<span style="white-space:pre">	</span>http://www.springframework.org/schema/context/spring-context.xsd">
<span style="white-space:pre">	</span><context:component-scan base-package="com.zscs.controller"/>
<span style="white-space:pre">	</span><!-- 配置渲染器 -->
<span style="white-space:pre">	</span><bean id="viewResolver"
<span style="white-space:pre">		</span>class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<span style="white-space:pre">		</span><property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<span style="white-space:pre">		</span><!-- 结果是视图的前缀 -->
<span style="white-space:pre">		</span><property name="prefix" value="/WEB-INF/jsp/"/>
<span style="white-space:pre">		</span><!-- 结果是视图的后缀 -->
<span style="white-space:pre">		</span><property name="suffix" value=".jsp"/>
<span style="white-space:pre">	</span></bean>
</beans>
五、在WEB-INFO 目录下新建一个文件夹文件夹名为jsp与上述渲染器配置中结果视图的前缀保持一致,并且在jsp目录下新建一个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 '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}
  </body>
</html>

六、编写控制器类HelloController

package com.zscs.controller;


import java.io.IOException;
import java.io.PrintWriter;


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 HelloController {
<span style="white-space:pre">	</span>@RequestMapping("/hello")
<span style="white-space:pre">	</span>public ModelAndView hello(HttpServletRequest arg0,
<span style="white-space:pre">			</span>HttpServletResponse arg1){
<span style="white-space:pre">		</span>ModelAndView mv=new ModelAndView();
<span style="white-space:pre">		</span>mv.addObject("msg", "hello SpringMVC");
<span style="white-space:pre">		</span>mv.setViewName("hello");
<span style="white-space:pre">		</span>return mv;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>@RequestMapping("/gg")
<span style="white-space:pre">	</span>public ModelAndView gg(HttpServletRequest arg0,
<span style="white-space:pre">			</span>HttpServletResponse arg1){
<span style="white-space:pre">		</span>ModelAndView mv=new ModelAndView();
<span style="white-space:pre">		</span>mv.addObject("msg", "gg");
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>PrintWriter out=arg1.getWriter();
<span style="white-space:pre">			</span>out.print("hello妮妮");
<span style="white-space:pre">		</span>} catch (IOException e) {
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>mv.setViewName("hello");
<span style="white-space:pre">		</span>return mv;
<span style="white-space:pre">	</span>}
}

七、发布到服务器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值