SpringMVC框架之HelloWorld

本文详细介绍了SpringMVC数据请求流程,并通过创建第一个SpringMVC工程,演示了一个用户输入字符串并显示在另一个页面的简单交互。步骤包括:创建动态Web工程,配置DispatcherServlet,编写hello.jsp页面,实现Controller,以及配置test1-servlet.xml文件。最后展示了如何通过配置使得请求映射和视图解析工作。
摘要由CSDN通过智能技术生成

一、SpringMVC数据请求流程
request –> DispatcherServlet –>Handler Mapping –>Control Servlet –>ModelAndView –>ViewResolver –> View –> response

其中 ModelAndView 会调用 数据层(db,cache….)此处略

二、创建一个第一个springMVC工程
(环境搭建此处略)
工程说明;
该工程主要是一个简单的交互,用户输入一个字符串,提交后将在另一个页面展示
1、用eclipse创建一个动态的Web工程, 在生成的时候注意勾选上自动生成web.xml文件,系统默认不自动生成
2、将Spring所需要的jar包copy到lib目录下。 (spring包可在相关网址下载)
3、在web.xml配置 DispatchServlet与HandlerMapping的名字。
配置如下:

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>test1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <!-- 配置调度器 -->
    <servlet-name>test1</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>

  <!-- 配置Handler Mapping -->
  <servlet-mapping>
    <!-- 注意 Bean Mapping必须在文 件 test1-servlet.xml在进行栩置 -->
    <servlet-name>test1</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

由于我的ServletName配置的是test1,那么必须新增一个test-servlet.xml文件。

该文件在最后列出,因为还有其它的配置需要说明。

4、创建hello.jsp页面,如下
响应事件为 hello.do, hello.do如何与控制器关键是在test1-servlet.xml中定义实现的

<%@ 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>Insert title here</title>
</head>
<body>
<form action="hello.do" method="post">
    hello:<input name="hello" type="text" />
    <input type="submit" value="提交" />
</form>

</body>
</html>

创建Controller, 如下所示
使用了最原始的方式(非注解),该控制器主要获取 “hello” 参数,然后再返回给index页面,实际是index.jsp 因为在test1-servlet配置页面解析器时,与置了后缀

package com.test1.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {
        String hello = arg0.getParameter("hello");
        System.out.println("arg0 = " + hello);

        ModelAndView mav = new ModelAndView("index");
        mav.addObject("helloworld", "hello:" + hello);

        return mav;
    }

}

创建view/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>Insert title here</title>
</head>
<body>
<h1>${helloworld }</h1>
</body>
</html>

注意:index.jsp必须在view,原因是因为在test1-servlet中配置了前置器为 /view/ 这意味首后续路径均以 /view/ 为根目录

test1-servlet.xml 如下:

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        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-3.0.xsd">

    <!--  固定配置,配置解年方式 -->
    <bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"> </bean>

    <!-- Bean 映身配置 -->
    <bean name="/hello.do" class="com.test1.Controller.HelloController"></bean>

    <!-- 页面解析器配置,配置前置解析器,后置解析器,主要用于路径拼节 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/view/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

此至,一个简单的helloworld工程完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值