志比天高

燕雀安知鸿鹄之志

导航

spring mvc 第一个程序搭建

今天刚开始学习spring mvc,建立了第一个spring mvc的小例子,分享给大家,希望有志之士共同学习进步。

不管spring mvc到底有多难理解,但是我们可以不考虑其原理,先通过简单的例子慢慢深入!

我用的是annotation注解来跑起第一个小程序的。

下面是构建spring mvc的配置:

我的项目名称是spring_mvc_001

1、首先配置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">   
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.form</url-pattern>
    </servlet-mapping>
</web-app>

2、然后配置dispatcher-servlet.xml

为什么要命名为dispatcher-servlet.xml呢,这里你又疑问可以暂时停一下,继续进行下面的内容,因为我也有疑问。

dispatcher-servlet.xml放在WEB-INF下面

配置如下:

<?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:mvc="http://www.springframework.org/schema/mvc"
 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"
 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/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 <!--
        使Spring支持自动检测组件,如注解的Controller
    -->
    <context:annotation-config />  
   
 <context:component-scan base-package="com.web.controllor" />

 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  p:prefix="/" p:suffix=".jsp" />
</beans>

3、配置文件配置好了,然后建立controller类

下面是我的代码:

@Controller
public class IndexController {
 @RequestMapping("/helloWorld")
 public ModelAndView helloWorld() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("helloWorld");
        mav.addObject("message", "Hello World!");
        return mav;
    }
}

4、现在呢就可以跑一下程序了,在地址栏里输入

http://localhost:8080/spring_mvc_001/helloWorld.form

然后你会进入到helloWorld.jsp的页面!当然你项目里要建立helloWorld.jsp

如果你现在跑成功了,恭喜你!

那么里面我们肯定遇到很多问题啊,因为跑成功了你都不知道为什么是这样?或者你的程序可能还没跑成功,没跑成功的也恭喜你,如果你看不懂我这篇文章

你就有多学习其他文章的机会!呵呵,咱们都不要悲观嘛,我也是今天刚自学的,写这篇文章就是对我学习的一个记录。不扯了,那么来解决上面遇到的问题吧。

问题一

<servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
</servlet>

到底是什么意思?

解决一:

DispatcherServlet 是Spring MVC 的入口
所有进入Spring Web 的 Request 都经过 DispatcherServlet
需要在 web.xml 中注册 DispatcherServlet

加载 DispatcherServlet 时 Spring 会尝试读取配置文件 ,问题来了,为什么spring的配置文件要是dispatcher-servlet.xml呢?

问题二:

为什么spring的配置文件要是dispatcher-servlet.xml呢?

解决二:

默认的spring配置文件位于 web.xml 相同的路径下 文件名与注册的 Servlet
名有关 Servlet注册名跟上 -servlet.xml ,那么问题解决了,我们的web.xml注册的servlet名字是dispatcher。

问题三:

<servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.form</url-pattern>
</servlet-mapping>

这段代码什么意思呢?

解决三:

注册 DispatcherServlet 后 还应指定有 Spring 处理的 url 模板 这样 请求 .form 的处理 就全部交由 Spring 处理了

问题四:

@Controller和@RequestMapping("/helloWorld")是干嘛的?

解决四:

@Controller注解标识一个控制器,@RequestMapping注解标记一个访问的路径

问题五:

为什么返回的是helloWorld.jsp呢?

解决五:

你看程序返回的字符串不就是"helloWorld"吗.

 

 

声明:由于我也是刚学,能力有限,可能说的还有错的,如果有错大家指出来共同改正,共同学习,谢谢!

posted on 2012-08-25 13:37  奥运  阅读(560)  评论(0编辑  收藏  举报