使用SpringMVC写一个简单的跳转界面

1.目录

在这里插入图片描述

2.在pom.xml中配置依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>1009_4_param</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
        <dependency>
            <!-- jstl 支持 -->
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <!-- servlet编译环境 -->
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <!-- jsp编译环境 -->
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.54</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <path>/shiro</path>
                    <port>8080</port>
                    <uriEncoding>UTF-8</uriEncoding>
                    <url>http://localhost:8080/shiro</url>
                    <server>Tomcat7</server>
                </configuration>
            </plugin>

            <!-- 编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

3.在main下创建webapp->WEB-INF->web.xml(这里用的是2.5版本的web.xml),此时springmvc.xml会报红,因为没写

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           version="2.5">

    <servlet>
        <servlet-name>mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 局部参数:声明配置文件位置 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <!-- Servlet启动时刻:可选 -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

4.在resources下配置springmvc.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: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="controller"></context:component-scan>

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


</beans>

5.在java下创建controller包->MyController

package controller;

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

/**
 * zt
 * 2020/10/9
 * 20:44
 */
@Controller
public class MyController {
    @RequestMapping("/hello")
    public String hello1(){
        System.out.println("开始跳转--->");
        return "hello2";
    }
}

6.在webapp下直接创建两个jsp文件(因为在这里创建可以直接访问)

hello.jsp

<%--
  Created by IntelliJ IDEA.
  User: 49841
  Date: 2020/10/9
  Time: 20:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>hello</title>
</head>
<body>
<a href="hello">跳转到controller,再由conteoller跳转到hello2界面</a>
</body>
</html>

hello2.jsp

<%--
  Created by IntelliJ IDEA.
  User: 49841
  Date: 2020/10/9
  Time: 20:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>hello2</title>
</head>
<body>
 这是hello界面跳转过来的另一个界面
</body>
</html>

7.运行结果

在这里插入图片描述
点击后跳转
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
Spring MVC是一个基于Java的Web框架,用于开发Web应用程序。它采用了MVC(Model-View-Controller)的设计模式,将应用程序的逻辑分为三个部分:模型(Model)、视图(View)和控制器(Controller)。 在Spring MVC中,登录界面的实现可以通过以下步骤完成: 1. 创建一个Controller类,用于处理登录请求和验证用户信息。可以使用`@Controller`注解标记该类,并使用`@RequestMapping`注解指定处理请求的URL路径。 2. 在Controller类中,创建一个方法用于显示登录页面。可以使用`@RequestMapping`注解指定该方法处理的URL路径,并返回一个逻辑视图名或视图对象。 3. 在登录页面中,使用HTML表单元素创建一个登录表单。表单中包含用户名和密码的输入框,并设置表单的提交路径为Controller类中处理登录请求的方法。 4. 在Controller类中,创建一个方法用于处理登录请求的提交。可以使用`@RequestMapping`注解指定该方法处理的URL路径,并使用`@RequestParam`注解获取表单中的用户名和密码参数。 5. 在登录请求提交方法中,进行用户信息的验证。可以通过调用服务层或数据库查询来验证用户输入的用户名和密码是否正确。 6. 根据验证结果,可以进行相应的处理。如果验证成功,可以跳转到主页或其他页面;如果验证失败,可以返回登录页面,并显示错误信息。 7. 在视图层中,根据控制器返回的逻辑视图名或视图对象,渲染相应的页面。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值