SpringMVC之HelloWorld

简介: SpringMVC之HelloWorld

SpringMVC概述


SpringMVC是优秀的MVC框架之一,通过一套MVC注解,让POJO成为处理请求的控制器,无须实现任何借口。


SpringMVC支持REST风格的URL请求,采用了松散耦合可插拔组件结构,比其他MVC框架更具扩展性和灵活性。


下面使用IDEA创建一个SpringMVC项目,项目结构如下:



HelloWorldController是一个控制器,用于处理请求:


package com.led.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * @author Alan
 * @date 2018/5/23 22:07
 * @Controller:表明这个是一个控制器
 * @RequestMapping:映射请求的路径
 * 返回值会通过视图解析器解析为实际的物理视图,对于InternalResourceViewResolver 视图解析器,会做如下解析:
 * 通过 prefix + returnval + 后缀的方式得到实际的物理视图,然后做转发操作
 * /WEB-INF/views/success.jsp
*/
@Controller
public class HelloWorldController {
    @RequestMapping("/helloWorld")
    public String helloWorld(){
        System.out.println("hello world");
        return "success";
    }
}


web.xml(项目的整体配置文件):


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--配置DispatcherServlet-->
    <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>/</url-pattern>
    </servlet-mapping>
</web-app>


dispatcher-servlet.xml(SprimgMVC相关的配置文件):


<?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"
       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">
    <!--配置自动扫描的包-->
    <context:component-scan base-package="com.led.controller"/>
    <!--配置视图解析器,把handler方法返回值解析为实际的视图-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

applicationContext.xml(Spring的整体配置文件):


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>


success.jsp(视图呈现页面):


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h4>Success page</h4>
</body>
</html>


运行项目,浏览器输入http://localhost:8080/springmvc/,点击超链接,会显示


Success page

SpringMVC运行原理图:


相关文章
|
4月前
|
Java 应用服务中间件 Maven
SpringBoot - HelloWorld与简解
SpringBoot - HelloWorld与简解
35 0
|
6月前
|
前端开发 Java Maven
SpringMVC 写个 HelloWorld
SpringMVC 写个 HelloWorld
|
8月前
|
Web App开发 前端开发 Java
SpringBoot 的项目helloworld--JSB系列之002
SpringBoot 的项目helloworld--JSB系列之002
|
10月前
|
Java Maven
springboot集成jsp的Demo实现
1.创建一个maven web项目 2.添加springboot和jsp相关依赖
springboot集成jsp的Demo实现
|
设计模式 前端开发 Java
SpringMVC的HelloWord
SpringMVC的HelloWord
80 0
SpringMVC的HelloWord
|
缓存 安全 Java
SpringBoot整合Shiro_HelloWorld
SpringBoot整合Shiro_HelloWorld
109 0
SpringBoot整合Shiro_HelloWorld
|
缓存 安全 算法
SpringSecurity笔记之helloworld
SpringSecurity笔记之helloworld
64 0
SpringSecurity笔记之helloworld
|
Java Maven Spring
【SpringBoot】SpringBoot2之编写第一个HelloWorld
【SpringBoot】SpringBoot2之编写第一个HelloWorld
|
Java 程序员 项目管理
SpringBoot入门系列HelloWorld
SpringBoot入门系列HelloWorld
106 0
SpringBoot入门系列HelloWorld
springMVC helloword示例
springMVC helloword示例
77 0