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";
}
}