前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Mvc 4 HelloWord

Spring Mvc 4 HelloWord

作者头像
三产
发布2021-01-13 10:56:18
2070
发布2021-01-13 10:56:18
举报

构建项目

引入jar包

  • spring-aop-4.2.0.RELEASE.jar
  • spring-beans-4.2.0.RELEASE.jar
  • spring-context-4.2.0.RELEASE.jar
  • spring-core-4.2.0.RELEASE.jar
  • spring-expression-4.2.0.RELEASE.jar
  • spring-web-4.2.0.RELEASE.jar
  • spring-webmvc-4.2.0.RELEASE.jar
  • commons-logging-1.2.jar

创建web.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <!-- Spring跳转Servlet配置 -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

创建springmvc.xml

springmvc.xml应与上方classpath:springmvc.xml匹配

代码语言:javascript
复制
<?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-4.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
    <!-- 配置自动扫描的包 -->
    <context:component-scan base-package="com.sanchan.server.controller"></context:component-scan>
    <!-- 配置视图解析处理器:将Controller方法返回解析为实际的物理视图 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

#创建Controller 在src下创建com.sanchan.server.controller.HelloWorld

代码语言:javascript
复制
package com.sanchan.server.controller;

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

//表示该类为Spring的处理器,监听相关操作
@Controller
public class HelloWorld {
    /**
     * 1、用@RequestMapping来映射请求的URL
     * 2、返回值会通过视图解析器解析为实际的物理视图,对于org.springframework.web.servlet.view.
     * InternalResourceViewResolver视图解析器,会做如下解析:
     *  prefix【在springmvc.xml中配置的prefix值】+下面return返回的值+suffix【在springmvc.xml中配置的suffix值】
     * @return
     */
    @RequestMapping("/helloworld")
    public String hello() {
        System.out.print("hello world");
        return "success";
    }
}

在WEB-INF下创建/views/success.jsp

这样就完成了一个简单的spring mvc

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015/08/08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 构建项目
    • 引入jar包
      • 创建web.xml
        • 创建springmvc.xml
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档