基于注解的SpringMVC

一.使用@Controller注解
1.在Spring MVC设计的WEB应用中,可以使用 @Controller 对控制器类(原本实现Controller的类)进行注解!
一旦使用了 @Controller 注解,控制器类可以不必实现Controller接口!

2.注意:@Controller注解不可以单独使用,必须匹配@RequestMapping一起使用!

二.使用@RequestMapping注解

1.@RequestMapping用于对处理器类中的方法进行注解,并且需要指定所处理的请求的路径
@RequestMapping("/register.do")
注意:使用了@Controller和@RequestMapping的组合之后不要配置HandlerMapping!!!

2.@RequestMapping用于对处理器类进行注解!
@RequestMapping("/user")
例如请求路径是:http://SERVER:PORT/PROJECT/user/register.do

3.@RequestMapping用于配置处理器类和请求路径的映射,必须对处理器类中的方法进行注解,
如果请求路径有多层级,可以添加对处理器类的注解,则在对方法的注解中可以少写一些路径。

三.基于注解的Spring MVC开发流程
1.创建项目并完成基础配置:创建项目、解决web.xml的问题、添加spring-webmvc依赖、在resources中粘贴applicationContext.xml、为项目添加Tomcat Runtime

2.在web.xml中配置DispatcherServlet(代码没有修改)
3.在applicationContext中配置组件扫描、ViewResolver,在配置ViewResolver请注意路径是否正确

4.设计请求:http://SERVER:PORT/PROJECT/user/login.do
5.创建用于处理请求的控制器类,使用@Controller进行注解,并使用@RequestMapping对路径(/user)进行注解:
6.在控制器类中添加处理请求的方法,并使用@RequestMapping映射到请求的资源:("/register.do")和("/login.do")

7.完善处理请求的方法,如果对于此次请求只需要转发到JSP页面,则处理方法的返回值类型设计为String即可!返回值就是最终负责显示的View组件的名称!暂时可以理解为JSP页面的文件名(不包含.jsp部分)!

8.设计JSP页面,注意:JSP页面的位置必须与Spring的XML配置文件中的前缀配置保持一致,同时,JSP页面的文件名与以上第7步返回值的字符串保持一致!

9.将部分添加到Server进行管理,重新部署到Tomcat,测试运行!

四.项目实际开发结果

访问user/login.do或user/register.do

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_2_5.xsd" version="2.5">
  <display-name>DAY04-01-SpringMVC-Annotation</display-name>
  <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>
  <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:applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

2.WEB-INF下的名为jsp文件下的以jsp为结尾的文件

index.jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>SpringMVC-HelloWorld</title>
</head>
<body>
	<h3>欢迎使用SpringMVC!</h3>
</body>
</html>

login.jsp文件

<%@ page language="java" 
	contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" 
	content="text/html; charset=utf-8">
	<title>Spring MVC</title>
</head>
	
<body>
	<h3>请登录!</h3>
</body>
</html>

3.resources文件下的Spring配置文件applicationContext.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:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

	<context:component-scan base-package="cn.tedu.spring"></context:component-scan>
	<!-- 配置HandlerMapping,不需要指定id属性 -->
	<!-- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				key表示请求的路径,文本节点配置Controller的bean id
				<prop key="/hello.do">helloController</prop>
			</props>
		</property>
	</bean> -->

	<!-- 配置ViewResolver -->
	<!-- 不需要指定id属性 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 前缀,即View文件在项目中相对于webapp的路径 -->
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<!-- 后缀,即文件的扩展名 -->
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>


4.控制器类

package cn.tedu.spring.controller;

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


@Controller
@RequestMapping("/user")
public class RegisterController {

	// http://SERVER:PORT/PROJECT/user/register.do
	@RequestMapping("/register.do")
	public ModelAndView handleRequest() throws Exception {
		System.out.println("RegisterController.handleRequest()");
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("index");
		
		return mav;
	}
	
	// http://SERVER:PORT/PROJECT/user/login.do
	@RequestMapping("/login.do")
	public String login() {
		return "login";
	}
	
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

linsa_pursuer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值