SpringMVC实战(一) —— HelloWorld

一、实现步骤
  • 1、 创建一个动态web工程项目, src中新建com.fc.springmvc.handlers 的包, 在该包下新建HelloWorld.java 类,编写处理请求的处理器, 并标识为处理器
  • 2、将jar包拖到 WebContent/WEB-INF/lib 目录下
    • commons-logging-xxx.jar
    • spring-aop-xxx.RELEASE.jar
    • spring-beans-xxx.RELEASE.jar
    • spring-context-xxx.RELEASE.jar
    • spring-core-xxx.RELEASE.jar
    • spring-expression-xxx.RELEASE.jar
    • spring-web-xxx.RELEASE.jar
    • spring-webmvc-xxx.RELEASE.jar
  • 3、在web.xml中加入DispatcherServlet
  • 4、在WebContent/WEB-INF 目录下新建spring的配置文件dispatcherServletDemo-servlet.xml
  • 5、在WebContent/WEB-INF 目录下新建views视图子目录并创建success.jsp 页面

二、代码实现步骤

1、HelloWorld.java

  • @Controller标识为处理控制器
  • 使用 @RequestMapping 注解来映射请求的 URL
package com.fc.springmvc.handlers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorld {
    /**
     * 1. 使用 @RequestMapping 注解来映射请求的 URL
     * 2. 返回值会通过视图解析器解析为实际的物理视图, 对于 InternalResourceViewResolver 视图解析器, 会做如下的解析:
     * 通过 prefix + returnVal + 后缀 这样的方式得到实际的物理视图, 然会做转发操作
     *
     * /WEB-INF/views/success.jsp
     *
     * @return
     */
    @RequestMapping("/helloworld")
    public String hello() {
        System.out.println("hello world!");
        return "success";
    }
}

2、web.xml
配置DispatcherServletDispatcherServlet 默认加载 /WEB- INF/<servletName-servlet>.xml 的Spring配置文件 , 启动WEB层的Spring容器。可以通过contextConfigLocation初始化参数自定义配置文件的位置和名称

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp_ID">

    <!-- 配置DispatcherServlet -->

    <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <servlet>
        <servlet-name>dispatcherServletDemo</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <!-- 配置 DispatcherServlet 的一个初始化参数: 配置 SpringMVC 配置文件的位置和名称 -->
        <!-- 
            实际上也可以不通过 contextConfigLocation 来配置 SpringMVC 的配置文件, 而使用默认的.
            默认的配置文件为: /WEB-INF/<servlet-name>-servlet.xml
        -->
        <!--  
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:xxx-servlet.xml</param-value>
        </init-param>
        -->

        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>dispatcherServletDemo</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

3、创建Spring MVC配置文件:dispatcherServletDemo-servlet.xml

  • 配置自定扫描的包
  • 配置视图解析器: 将视图逻辑名解析为: /WEB-INF/views/<viewName>.jsp
<?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: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-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!-- 配置自定扫描的包 -->
    <context:component-scan base-package="com.fc.springmvc"></context:component-scan>

    <!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>
三、问题和解决方案
  • 1、Eclipse中web.xml 不能自动补全springmvc DispatcherServlet
    解决方案: 在web.xml中先输入左尖括号(<),然后再删除这个符号,就可以在提示信息中翻到最下边儿就看到了#springDispatcherServlet
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值