springMvc的Hello world(xml配置)

本章主要介绍与Struts2类似的mvc框架SpringMvc,讲解怎么建立第一个helloworld程序的建立

讲解不到位的地方欢迎大家指正:联系方式rlovep.com

详细请看源代码注释:

写文章不易,欢迎大家采我的文章,以及给出有用的评论,当然大家也可以关注一下我的github;多谢;

1.下载spring的release包

地址:http://repo.springsource.org/libs-release-local/org/springframework/spring/

2.建立一个动态web应用

  1. 建立名为springmvc的web应用
  2. 导入必须要的包:我使用的是spring4.2,需要导入如下包:

    org.springframework.beans-4.2.3.RELEASE.jar

    org.springframework.context-4.2.3.RELEASE.jar

    org.springframework.core-4.2.3.RELEASE.jar

    org.springframework.expression-4.2.3.RELEASE.jar

3.在/WEB-INF/下修改或创建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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMvc</display-name>
     <!-- 注册springMvc核心控制器 -->
     <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     </servlet>
     <!-- 配置映射 -->
     <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>*.action</url-pattern>
     </servlet-mapping>
         <!-- 首页配置 -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

4.建立HelloControl.java类并实现Controller接口

package com.rlovep.hello;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloControl implements Controller{

    public HelloControl() {
        System.out.println("创建helloControl");
    }
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("进入处理方法");
        /**
         * ModelAndView表示向视图封装的数据和真实路径
         */
        ModelAndView modelAndView=new ModelAndView();
        //写入一个域对象
        modelAndView.addObject("message","HelloWorld");
        //真实路径
        modelAndView.setViewName("/jsp/hello.jsp");
        return modelAndView;
    }

}

5.在/webRoot/或者/webContent/下建立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>第一个springMvc程序</title>
</head>
<body>
${message }
</body>
</html>

6.在/WEB-INF/创建DispatcherServlet-servlet.xml配置文件

  1. xml头部信息与spring.xml相同
  2. 命名规则:web.xml文件中配置的的值-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: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.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx.xsd
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc.xsd
         ">
         <bean name="/hello.action" class="com.rlovep.hello.HelloControl"></bean>
</beans>   

7.部署web应用到tomcat中,通过浏览器访问如下URL:http://localhost:8080/springmvc/hello.action

显示如下:

8.注意spring的控制器类是单例的

只会构造一次,但是会重复执行方法,注意与Struts2的区别;

上图的测试可以说明控制器是单例的。

springmvc与struts2主要区别:

1)springmvc的入口是一个servlet,即前端控制器,例如:*.action
   struts2入口是一个filter过虑器,即前端过滤器,例如:/*
2)springmvc是基于方法开发,传递参数是通过方法形参,可以设计为单例
   struts2是基于类开发,传递参数是通过类的属性,只能设计为多例
3)springmvc通过参数解析器是将request对象内容进行解析成方法形参,将响应数据和页面封装成
   ModelAndView对象,最后又将模型数据通过request对象传输到页面
struts采用值栈存储请求和响应的数据,通过OGNL存取数据

好的本章介绍到这里

来自伊豚wpeace(rlovep.com)

时间: 2024-08-24 12:28:51

springMvc的Hello world(xml配置)的相关文章

Spring+SpringMVC+MyBatis+Maven 服务端XML配置

项目目录结构 spring-mybatis.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.spr

SpringMVC 返回JSON和JSP页面xml配置

SpringMVC 返回JSON和JSP页面xml配置 代码1: <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Enables the Spring MVC @Controller programming model --> <annotation-driven /> <!-- Handles HTTP GET re

SpringMvc的xml配置与annotation配置的例子的区别

1.导入jar包时,要在xml配置基础上加 spring-aop-4.2.2.RELEASE.jar (注解的时候需要) 2.编写controller的时候要annotation需要做相关配置即红色部分,而xml就是要实现controller的接口 (a)annotation配置时 package com.spring.hello; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServle

Springmvc案例1----基于spring2.5的采用xml配置

首先是项目和所需要的包截图: 修改xml文件: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLoca

springmvc深入学习----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.or

Maven-SSM项目pom.xml配置以及springmvc配置以及mybatis配置

一.Maven本地仓库的pom.xml配置 (全部是mysql数据库) 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/ma

springmvc.xml和applicationContext.xml配置的特点

1:springmvc.xml配置要点 一般它主要配置Controller的组件扫描器和视图解析器 下为:springmvc.xml文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-

springmvc web.xml配置之SpringMVC IOC容器初始化

SpringMVC IOC容器初始化 首先强调一下SpringMVC IOC容器初始化有些特别,在SpringMVC中除了生成一个全局的spring Ioc容器外,还会为DispatcherServlet生成一个容器,具体的下一篇有讲述. 我们知道spring中单纯使用main函数就可以生成一个容器,如下: public class MainTest { public static void main(String[] args){ ApplicationContext appContext =

spring mvc 配置(xml配置详解)

如果您曾经使用Spring MVC框架开发过Web应用程序,本文提供关于Spring MVC框架的配置技巧,以帮助管理基于Spring的web应用程序的多个实例. Spring Framework(J2EE框架),Spring(Spring框架)下载 2013-08-26Spring Framework(J2EE框架) 3.2.4 2013-08-26Spring(Spring框架) 4.0.0.M2 web.xml 配置: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <s

基于xml配置的springMVC-快速入门

一:准备工作 将需要用到的spring包放进lib文件夹内,具体需要什么包自己网上查,或在我的例子里找. 二:配置文件:web.xml 主要是配置servlet路径方式和指定上下文xml文件. 1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="