springmvc快速入门(注解版本)

1)springmvc快速入门(传统版)

   步一:创建springmvc-day02这么一个web应用

   步二:导入springioc,springweb和springmvc相关的jar包

  ------------------------------------------------------springWEB模块
   org.springframework.web-3.0.5.RELEASE.jar
org.springframework.web.servlet-3.0.5.RELEASE.jar(mvc专用)
   ------------------------------------------------------springIOC模块
   org.springframework.asm-3.0.5.RELEASE.jar
   org.springframework.beans-3.0.5.RELEASE.jar
   org.springframework.context-3.0.5.RELEASE.jar
   org.springframework.core-3.0.5.RELEASE.jar
   org.springframework.expression-3.0.5.RELEASE.jar

步三:在/WEB-INF/下创建web.xml文件

<servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

步四:创建HelloAction.java控制器类

@Controller
public class HelloAction{
    @RequestMapping(value="/hello")
    public String helloMethod(Model model) throws Exception{
        System.out.println("HelloAction::helloMethod()");
        model.addAttribute("message","这是我的第二个springmvc应用程序");
        return "/success.jsp";
    }    
}

步五:在/WebRoot/下创建success.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>这是我的第二个springmvc应用程序</title>
  </head>
  <body>
    success.jsp<br/>
    ${message}
  </body>
</html>

步六:在/src/目录下创建spring.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:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
        
      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
        
      ">
        

      <!-- Action控制器 -->
      <context:component-scan base-package="loaderman.javaee.springmvc.helloannotation"/>      

      
      
      <!-- 基于注解的映射器(可选) -->
      <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
      
      <!-- 基于注解的适配器(可选) -->
      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
      
      <!-- 视图解析器(可选) -->
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
          
</beans>

步七:部署web应用到tomcat中,通过浏览器访问hello.action的URL

posted on 2018-12-04 11:52  LoaderMan  阅读(592)  评论(0编辑  收藏  举报

导航