SpringMVC入门(基于注解方式实现)

---------------------siwuxie095

   

   

   

   

   

   

   

SpringMVC 入门(基于注解方式实现)

   

   

SpringMVC 通过一套 MVC 注解,让 POJO 无需实现任何接口即可成

为处理请求的控制器,具体实现如下:

   

   

1、在部署描述文件中进行配置

   

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" version="3.1">

<display-name>TestSpringMVC</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 可任意命名 -->

<servlet-name>dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!--

设置 SpringMVC 核心配置文件的名称和位置,均可任意。如果未设置,则默认

位于 WEB-INF 目录下,名称为 [servlet-name]-servlet.xml

-->

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:dispatcher-servlet.xml</param-value>

</init-param>

<!-- 自动加载:随 Tomcat 容器启动,加载 DispatcherServlet,完成初始化 -->

<load-on-startup>1</load-on-startup>

</servlet>

 

<servlet-mapping>

<servlet-name>dispatcher</servlet-name>

<!-- url-pattern 可以是 / *.xxx /xxx/*,不能是 /* -->

<url-pattern>*.do</url-pattern>

</servlet-mapping>

   

 

</web-app>

   

   

   

2、编写一个 Controller 类

   

HelloController.java:

   

package com.siwuxie095.controller;

   

import org.springframework.stereotype.Controller;

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

import org.springframework.web.servlet.ModelAndView;

   

/**

* HelloController 加上注解 @Controller,即可变成一个 Controller

* (无需再实现 Controller 接口)

*/

@Controller

//@RequestMapping("/user")

public class HelloController {

   

/**

* 给方法加上 @RequestMapping 注解来设置访问路径(请求路径)

*

* @RequestMapping("/hello") 等同于 @RequestMapping(value="/hello")

*

* 当然,类也可以加上 @RequestMapping 注解,这样访问路径就多了一环。

* 如:给类上加 @RequestMapping("/user"),则访问路径为 /user/hello

*

* 另外:@RequestMapping 注解中的斜杠 / 可以省略不写,建议写上

*/

@RequestMapping("/hello")

public ModelAndView hello() {

// 创建 ModelAndView 对象,并设置视图名称

ModelAndView mv = new ModelAndView("hello");

// 添加模型数据

mv.addObject("msg", "Hello SpringMVC Annotation");

return mv;

}

   

 

}

   

   

   

3、在 SpringMVC 核心配置文件中进行配置

   

dispatcher-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:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.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.xsd">

 

 

<!-- 配置 HandlerMapping(可选,即 可以省略不配置) -->

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

 

<!-- 配置 HandlerAdapter(可选,即 可以省略不配置) -->

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

 

<!--

配置 Controller(必须,即 必须进行配置)

 

class 为自定义 Controller 类的完全限定名,这里通过 Controller

类中的 @RequestMapping 注解来设置访问路径(请求路径)

 

使用注解时,另一种配置 Controller 的方式:配置扫描包

<context:component-scan base-package="com.siwuxie095.controller" />

-->

<bean class="com.siwuxie095.controller.HelloController"/>

 

<!-- 配置 ViewResolver(必须,即 必须进行配置) -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<!--

配置视图解析的前缀 prefix 和后缀 suffix

1)前缀:如果在 WebContent 目录下,则为 /,如果在 WEB-INF 目录下,则为 /WEB-INF/

2)后缀:一般为 JSP 文件,所以为 .jsp

 

例如:prefix="/"suffix=".jsp"viewname="test",则:"/test.jsp"

-->

<property name="prefix" value="/"/>

<property name="suffix" value=".jsp"/>

</bean>

   

 

</beans>

   

   

   

4、编写一个 JSP 页面

   

hello.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>hello</title>

</head>

<body>

   

<h1>${msg}</h1>

 

</body>

</html>

   

   

   

5、访问路径

   

http://localhost:8080/工程名/hello.do

   

   

   

   

   

   

   

   

   

   

【made by siwuxie095】

原文地址:https://www.cnblogs.com/siwuxie095/p/8486346.html