SpringMVC之“HelloWorld”起步

来源:互联网 发布:正能量软件 编辑:程序博客网 时间:2024/05/16 02:24

添加jar包
在eclipse中创建一个web应用程序后,向“WEB-INF\lib”中添加以下SpringMVC必须的jar包:
- commons-logging-1.2.jar
- spring-aop-4.3.10.RELEASE.jar
- spring-beans-4.3.10.RELEASE.jar
- spring-context-4.3.10.RELEASE.jar
- spring-core-4.3.10.RELEASE.jar
- spring-expression-4.3.10.RELEASE.jar
- spring-web-4.3.10.RELEASE.jar
- spring-webmvc-4.3.10.RELEASE.jar
这里写图片描述

配置DispatcherServlet
在web.xml中配置DispatcherServlet

<?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>SpringMVC</display-name>    <servlet>      <servlet-name>HelloWeb</servlet-name>      <servlet-class>         org.springframework.web.servlet.DispatcherServlet      </servlet-class>      <init-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:spring.xml</param-value>      </init-param>      <load-on-startup>1</load-on-startup>   </servlet>   <servlet-mapping>      <servlet-name>HelloWeb</servlet-name>      <url-pattern>/</url-pattern>   </servlet-mapping></web-app>
  • init-param:代表初始化参数,其中定义了spring的配置文件为classpath下的spring.xml;
  • load-on-startup:表示这个Servlet是这个项目在被加载时创建

编写请求处理器
请求处理器类:
这里写图片描述

package com.gisxx.handlers;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class HelloWorld {    @RequestMapping("/helloworld")    public String hello(){        System.out.println("hello,GISXXCOM!!");        return "success";    }}
  • @Controller:将类标识为控制器;
  • @RequestMapping("/helloworld"):映射请求路径为/helloworld;

编写写处理器配置文件。
因为在web.xml中指定了spring配置文件位置,所以在src中新建spring.xml文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"   xmlns:context="http://www.springframework.org/schema/context"   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-3.0.xsd   http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-3.0.xsd">   <context:component-scan base-package="com.gisxx.handlers" />   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">      <property name="prefix" value="/WEB-INF/jsp/" />      <property name="suffix" value=".jsp" />   </bean></beans>
  • context:component-scan:指定自动扫描包;
  • bean:配置视图解析器。prefix代表请求返回结果的前缀,suffix代表返回后缀

编写视图页面
在WebRoot根目录下新建index.jsp请求页面

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">    <title>My JSP 'index.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>  <body>    <a href="helloworld">hello world</a>  </body></html>

在控制器类中指定了请求路径为/helloworld,所以这里a标签的链接可以直接为“helloworld”

在WEB-INF中新建jsp文件夹,再在其中新建返回页面success.jsp(在控制器配置文件spring.xml中指定了前缀和后缀,再加上控制器返回字符串组成完整结果)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">    <title>My JSP 'success.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>  <body>   <h1> 微信公众号:GISXXCOM  </body></html>

在服务器中运行后,点击index.jsp超链接可以条状到success.jsp页面,控制台返回:hello,GISXXCOM

本文首发于微信公众号:GISXXCOM
这里写图片描述

原创粉丝点击