springMVC 入门程序 helloworld!

(本人还只是一个新手,如有什么不正确之处,希望各位能够指出)
springMVC是一个目前市场上日渐变火的一款框架,springmvc的核心配置其实还是来自于spring里头,在这里我使用的是springmvc3.0的版本和myeclipse2015工具。
在学习使用一门框架的时候,我们一般都是按照以下的顺序去进行的:
1.导入jar包
2.添加配置
3.编写相应的java类
4.运行测试
首先我们在myeclipse里面新建一个web项目,然后倒入相应的jar包:

这里写图片描述

接下来,我们开始编写配置文件。首先我们看到web.xml,在web.xml配置里面有一个叫做DispatcherServlet的分发器,他可以将前端页面发送过来的每一个请求都做一次转发。这个时候,我们可以在配置里面添加一个初始化的值contextConfigLocation,这个是一个特定的称号,意思是指配置文件的放置位置在哪里。

<?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" version="3.0">
  <!-- 当在这里初始化之后,DispatcherServlet会去读取一个[servlet-name]-servlet.xml -->
   <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVC-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

接下来便是springMVC-servlet.xml的配置了(这里要注意,springmvc框架里面对于管理控制器类的配置文件都有一种默认的命名规范是“[servlet-name]-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: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">
    <bean name="/helloworld" class="com.sise.lh.controller.Controllers">
    </bean>
</beans>

在springMVC-servlet.xml配置里面,有一个叫做bean的标签用于管理外部访问的内容,
bean里面包含了id和name两个属性,这里头其实对其的设置是有一定的讲究的;我们不妨可以回顾一下struts这款框架的设计,当一个请求从外部访问一个action的时候都是先通过web.xml,然后根据里面写好的一个分发器通过相应的name来访问相应的action。这一点原理,springmvc在设计的时候和struts很像,bean里面的name值是针对容器外部访问的值,也就是浏览器顶部的url里面输入的内容来进行是别的。
再回想一下spring框架里面的设计,每一个bean在spring容器里面都是有用到一个id来作为一个唯一标识,但是id具有一定的安全性,它只是在提供给spring容器里面内部访问的。在这里,因为要给web页面的用户去访问,所以我设置的是name而不是id。

最后是控制器类(有点类似于struts2里面的action)

package com.sise.lh.controller;

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 Controllers implements Controller{

@Override
public ModelAndView handleRequest(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    response.getWriter().print("HelloWorld!");
    return null;
}

}

最终下来,整个项目的结构如下图:
这里写图片描述
这个时候我们在浏览器的顶端输入url即可进行测试:

这里写图片描述
结果和预期的一样,最终在浏览器上出现了“HelloWorld!”这样的内容

猜你喜欢

转载自blog.csdn.net/danny_idea/article/details/75324680