spring MVC注解之入门

jopen 10年前

第一步: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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  id="WebApp_ID" version="2.5">  <display-name>springmvc-chapter2</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的分发以及对springMVC文件的初始路径的引用 -->  <servlet>      <servlet-name>chapter1</servlet-name>      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>          <init-param>              <param-name>contextConfigLocation</param-name>              <param-value>classpath*:config/chapter1-servlet.xml</param-value>          </init-param>        <!-- 标识启动容器时初始化该Servlet -->      <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>      <servlet-name>chapter1</servlet-name>      <url-pattern>/</url-pattern>  </servlet-mapping>    <!-- 配置系统的编码集合 -->  <filter>      <filter-name>CharacterEncodingFilter</filter-name>      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>      <init-param>          <param-name>encoding</param-name>          <param-value>utf-8</param-value>      </init-param>  </filter>  <filter-mapping>      <filter-name>CharacterEncodingFilter</filter-name>      <url-pattern>/*</url-pattern>  </filter-mapping>

第二步:springMVC文件的配置,chapter1-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:p="http://www.springframework.org/schema/p"  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org        /schema/context"  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-3.0.xsd                       http://www.springframework.org/schema/mvc                      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">        <!-- 开启注解,因为每次启动都会调用下面两个类,所以springMVC将其集成到一个配置里面,配置了      这个,下面两个启用注解的类就可以不配置了 -->      <mvc:annotation-driven />        <!-- 此配置的作用是用来扫描所有需要注解的类,只有被扫描的类才能使用注解 -->      <context:component-scan base-package="cn.javass.chapter2.web.controller"/>        <!-- 启用注解,这两个类是使用注解时必须所配置的类,第一个作用是通过url找到类,第二个作用是通过类找到方法-->      <!--    <bean          class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>               <bean          class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean> -->        <bean id="viewResolver"          class="org.springframework.web.servlet.view.InternalResourceViewResolver">          <property name="prefix" value="/WEB-INF/jsp/"></property>          <property name="suffix" value=".jsp"></property>      </bean>

这个配置文件,如果一些配置报错,那么极有可能是xml文件的头部的那个schema验证有问题,到网上找些正确的就OK啦!

第三步:新建一个jsp页面,hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>  <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  <!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 World</title>  </head>  <body>  <c:forEach items="${map }" var="m">      <c:out value="${m.value }">${m.value }</c:out>      ${m.key }----------->${m.value }  </c:forEach>  </body>  </html>

第四步:新建一个Controller,HelloWorldController.java

package cn.javass.chapter2.web.controller;    import java.util.HashMap;  import java.util.Map;  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpServletResponse;  import org.springframework.web.bind.annotation.RequestMapping;  import org.springframework.web.bind.annotation.RequestMethod;  import org.springframework.web.servlet.ModelAndView;  import org.springframework.stereotype.Controller;    @Controller  public class HelloWorldController{  @RequestMapping(value="/hello",method=RequestMethod.GET)  public ModelAndView handleRequest(HttpServletRequest request,          HttpServletResponse response) throws Exception {      // 1、收集参数、验证参数      // 2、绑定参数到命令对象n      // 3、将命令对象传入业务对象进行业务处理      // 4、选择下一个页面      /*ModelAndView mv = new ModelAndView();      // 添加模型数据 可以是任意的POJO对象      mv.addObject("message", "This is the first test Hello World!");      // 设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面      mv.setViewName("hello");*/        System.out.println("进入此方法!");      Map<String,Object> map = new HashMap<String, Object>();      map.put("map1", "springMVC测试1");      map.put("map2", "springMVC测试2");      map.put("map3", "springMVC测试3");      return new ModelAndView("/hello","map",map);      }  }

@Controller注解写了之后(必须),Spring都会把它当做需要注入的Bean加载在上下文中,从而代替之前配置文件的功能;@RequestMapping(value=“/hello”,method=RequestMethod.GET),value代表访问路径,method代表请求方式,此处用GET请求,换成POST请求也可以。最后返回到hello.jsp视图。

启动tomcat,访问http://localhost:8080/springmvc-chapter2/hello,结果如下:springMVC注解之入门

至此,一个简单的springMVC注解就完成了!