欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【SpringMVC】快速入门(注解版本)(十)

程序员文章站 2022-07-15 11:10:44
...


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

@Controller:表示当前类是控制类

@RequestMapping(value="/hello")只能方法的访问映射地址,可以省略.action

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

步二:导入springiocspringwebspringmvc相关的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="cn.lfsenior.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中,通过浏览器访问如下URL

       http://127.0.0.1:8080/springmvc-day02/hello.action