SpringMVC篇:开启一个SpringMVC程序

1、加入jar包

 2、配置web.xml文件。然后需在WEB-INF下创建一个Springmvc的配置文件,文件名为<servlet-name>-servlet形式。如:dispatcherServlet-servlet.xml。

<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

3、在SpringMVC配置文件下,配置自动扫描的包。配置视图解析器。

<?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="com.caiyingbin.controller"></context:component-scan>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

4、创建前端处理器(@Controller)。和映射请求(@RequestMapping)

@Controller
public class MyController {
    @RequestMapping(value = "/book")
    public String getPut(){
        System.out.println("你成功了吧");
        return "success";
    }
}

5、若初始页面访问"book",可经前端处理器请求转发到/WEB-INF/pages/success.jsp页面。(根据视图解析器的配置)。

如:index.jsp

<form action="book" method="post">
    <input type="submit" value="按钮" >
  </form>

success.jsp

<%--
  Created by IntelliJ IDEA.
  User: Hasee
  Date: 2021/7/27
  Time: 11:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java"  isErrorPage="true" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
你币成功了
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值