复习:springmvc使用步骤

Springmvc使用步骤:
先在web.xml里面配置DispatcherServlet(这里可以开启utf-8的过滤)
----->

配置完毕后在resources下新建一个springmvc-xxx.xml文件,然后在里面开启注解扫描开启映射器和适配器,过滤静态资源,开启视图解析器
------>

编写Controller文件然后在上面用上注解@Controller和@GetMapping("/t1")

第一步:

<?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_4_0.xsd"
         version="4.0">
        <servlet>
            <servlet-name>DispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                    <param-value>classpath:springmvc-test.xml</param-value>
            </init-param>
        </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

第二步:

<?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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--       开启注解扫描 -->
    <context:component-scan base-package="com.zsp.controller"></context:component-scan>
<!--    开启映射器和适配器-->
    <mvc:annotation-driven></mvc:annotation-driven>
<!--    开启过滤静态资源-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--    视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

第三步

package com.zsp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Controller
public class ControllerTest1 {
//    @RequestMapping(value = "/t1/{a}/{b}",method = {RequestMethod.GET})
//    public String test1( @PathVariable int a ,@PathVariable int b,Model model){
//        int sum =a+b;
//        model.addAttribute("msg","这是用注解写的mvc"+sum);
//        return "test";
//    }
    @GetMapping("/t1/{a}/{b}")
    public String test2( @PathVariable int a ,@PathVariable int b,Model model){
        int sum =a+b;
        model.addAttribute("msg","这是用注解写的mvc"+sum);
        return "test";
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值