SpringMVC 简单使用

SpringMVC 用于接收前端请求并回送响应,DispatcherServlet负责根据请求的不同交与对应的Controller进行处理,Controller处理后回送View信息,ViewResolver 解析出相应的View视图然后DispatcherServlet响应给前端。

项目结构图:
SpringMVC简单应用项目图

lib:
SpringMVC依赖Jar包

前端展示信息index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello SpringMVC</title>
</head>
<body>
    <a href="helloSpring">Hello SpringMVC</a>
    <a href="springBackup">Hello SpringMVC</a>
</body>
</html>

配置信息:

/WEB-INF/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"
    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">


    <!-- 配置DispatchcerServlet -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置Spring mvc下的配置文件的位置和名称,默认为/WEB-INF/[servlet-name]-servlet.xml -->
        <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>

     <!--  修改SpringMVC配置文件路径的另一种方式
    <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:springmvc.xml</param-value>
    </context-param>

    <listener>
       <listener-class>
          org.springframework.web.context.ContextLoaderListener
       </listener-class>
    </listener>
    -->
</web-app>

src/springmvc.xml(src目录下的文件发布时会放在 WebContent/WEB-INF/classes 下,即classpath)

<?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">


        <!-- 配置自动扫描的包(扫描Annotation) -->
        <context:component-scan base-package="com.sxt.springmvc"></context:component-scan>

        <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- view的路径 -->
            <property name = "prefix" value="/WEB-INF/views/"></property>
            <!-- view的后缀 -->
            <property name = "suffix" value = ".jsp"></property>
        </bean>
</beans>

java代码:

package com.sxt.springmvc.handlers;

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

/**
 * 
 * Controller类
 * @author MoralIntegrity
 */
@Controller
public class SpringHello {

    /*
     *  请求为helloSpring时的处理方法
     */
    @RequestMapping("/helloSpring")
    public String hello(ModelMap model){
        System.out.println("hello SpringMVC");
        model.addAttribute("info", "hello SpringMVC ,I am a coder.");

        //会根据springmvc.xml的配置,解析出/WEB-INF/views/hello.jsp视图
        return "hello";
    }

    /*
     *  请求为springBackup时的处理方法
     */
    @RequestMapping("/springBackup")
    public String backup(){
        System.out.println("back used.");
        //会根据springmvc.xml的配置,解析出/WEB-INF/views/hello.jsp视图
        return "hello";
    }
}

hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8 "
    pageEncoding="UTF-8" isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>SpringMVC</title>
</head>
<body>
<h4>
<!-- helloSpring请求时会显示出EL表达式的信息, springBackup请求不会-->
${info}
</h4>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值