使用Spring框架,实现页面之间的跳转,返回Hello World

使用Spring框架,实现页面之间的跳转,返回Hello World

本次实验的目的是,通过一个页面点击超链接实现跳转到另一个页面,并显示hello world

经过研究,我认为这是一个体现spring框架的最简单的小程序。
在程序的实现过程中,在开始的页面,点击超链接,接着在服务器的控制层对映射到了超链接,并执行跳转到另外的hello界面。
整个过程如下:
step 1:加载index.jsp页面后初始界面。
在这里插入图片描述

step 2:点击超链接之后,跳转到hello界面。
在这里插入图片描述
而整个项目的结构框架如下:
在这里插入图片描述
注意:我这边的部署方式也不是太对,正常com包下面是公司名包,在下一级是项目名包,项目名包下面才会出现dao层,controller层,service层,entity层这样的部署方式才是比较严谨的。
同时,项目的跳转是通过服务器来实现访问hello界面的,直接通过网址访问是实现不了的。外部无法直接访问WEB-INF文件夹下的jsp文件。

现方式如下:
STEP1:在index中加载初始界面,代码如下:

<%@ 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>
    <a href="${path}/hello">Hello World</a>
</body>
</html>

STEP2:controller层接收映射,代码如下:

package com.jump.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorld{
    @RequestMapping("/hello")
    public String hello(){
        System.out.println("I am controller Hello!!");
        return "success";
    }
}

STEP3:跳转到success,代码如下:

<%--
  Created by IntelliJ IDEA.
  User: Yang0126
  Date: 2019/6/27
  Time: 18:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
      <h4>Hello World</h4>
</body>
</html>

其中spring框架下的代码如下:
applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>

spring-mvc.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.3.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

<!-- 1.注解扫描位置-->
<context:component-scan base-package="com.jump.controller" />

<!-- 2.配置映射处理和适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

<!-- 3.视图的解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>
</beans>

其中主要是要注意扫描的位置信息,不要写错。

STEP4:配置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">
  <display-name>Spring_MVC_Sum</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </context-param>

  <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:spring/spring-mvc.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>

在写代码的过程中,我对于spring下的两个配置文件,applicationContext.xml以及spring-mvc.xml之间的区别以及到底有什么用处还是不太了解,后来我去查阅了一些信息,也大致明白了一些。

对于这两者:
applicationContext.xml

application-context.xml是全局的,应用于多个serverlet,配合listener一起使用,web.xml中配置如下:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

spring-mvc.xml
spring-mvc.xml 是spring mvc的配置,web.xml中配置如下:

<!--配置springmvc DispatcherServlet-->
<servlet>
  <servlet-name>springMVC</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:config/spring-mvc.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  <async-supported>true</async-supported>
</servlet>

application-context.xml一般是采用非spring mvc架构,用来加载Application Context。
如果直接采用SpringMVC,只需要把所有相关配置放到spring-mvc.xml中就好,一般spring mvc项目用不到多个serverlet。

若有不对感激指正!!!

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值