ssm:第一个spring mvc 注解式项目

博主的第一个spring mvc项目是基于配置式的开发,有助于我们理解spring mvc的基本原理与流程,spring mvc还提供了注解式的开发流程,实际开发中常使用注解式开发程序。

相关实例:第一个sprin mvc项目

1.spring mvc-----配置式开发的关键代码

<!-- 1.配置处理映射器,确定一种请求~响应的映射规则 -->
	<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandleMapping"></bean>
	
	<!-- 2.配置处理器适配器,配置对处理器handlerequest()方法的调用--> 
	<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>	

	<!-- 3.配置自定义控制器,一条具体的映射关系,name对应客户端url请求,class对应服务端响应程序-->
	<bean name="/hello.do" class="com.xiaochen.controller.FirstController"></bean>
	
	<!-- 4.配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置视图前缀 -->
		<property name="prefix" value="/jsp/"/>
		<!-- 配置视图后缀,匹配模式:前缀+逻辑视图+后缀,形成完整路径名 -->
		<property name="suffix" value=".jsp"/>
	</bean>
	
	若使用默认的处理器映射器与处理器适配器,则可将这两步的配置流程省略
	若不配置视图解释器,则必须在控制器中配置完整的视图名称

2.spring mvc-----注解式开发的关键代码

	springmvc.xml文件
	<!-- 配置组件扫描器,扫描指定包中的注解 -->
	<context:component-scan base-package="com.xiaochen.controller"/>
	
	
	FirstController.java文件
	//使用@Controller注解将本类定义为一个控制器类,这个类无须再实Controller接口
	@Controller
	public class FirstController {
	//使用@RequestMapping注解定义一种“请求/响应”的映射关系,以此来取代之前处理器映射器和Bean的配置
	@RequestMapping("/hello.do")
	public ModelAndView doHello() {}
	
	}

应用实例:用注解式开发设计一个项目,在浏览器中输入http://localhost:8080/spring02/hello.do时,输出网页内容"My First Annotation SpringMVC!!!"

项目目录结构图
在这里插入图片描述

FirstController.java

package com.xiaochen.controller;

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

//使用@Controller注解将本类定义为一个控制器类,这个类无须再实现Controller接口
@Controller
public class FirstController {
	//使用@RequestMapping注解定义一种“请求/响应”的映射关系,以此来取代之前处理器映射器和Bean的配置
	@RequestMapping("/hello.do")
	public ModelAndView doHello() {
		ModelAndView mv=new ModelAndView();
		mv.addObject("msg","My First Annotation SpringMVC!!!");
		mv.setViewName("welcome");
		return mv;
	}
	
}

springmvc.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx.xsd">	
	
	<!-- 4.配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!--逻辑视图前缀-->
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<!--逻辑视图后缀,匹配模式:前缀+逻辑视图+后缀,形成完整路径名-->
		<property name="suffix" value=".jsp"></property> 
	</bean>
	
	<!-- 配置组件扫描器,扫描指定包中的注解 -->
	<context:component-scan base-package="com.xiaochen.controller"/> 
	
</beans>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>${msg }</h1>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>springmvc1</display-name>
  <!--  以下代码为新加上去的 -->
  <servlet>
	  <servlet-name>springmvc</servlet-name>	  
	  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	  <init-param>
		  <param-name>contextConfigLocation</param-name>
		  <param-value>classpath:springmvc.xml</param-value>
	  </init-param>
  </servlet>
  <servlet-mapping>
	  <servlet-name>springmvc</servlet-name>
	  <url-pattern>*.do</url-pattern>  
  </servlet-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>

运行结果图
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值