springmvc实现

一、 实现方式
① 基于xml
② 基于注解
二、配置springmvc
1、配置web.xml的servlet的转发类

点击查看代码
   <?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">
    
    <!--springmvc的核心控制器-->
    <servlet>
        <servlet-name>disp</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>
    <servlet-mapping>
        <servlet-name>disp</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>
2、 在servlet的转发类的初始化参数中, 配置 spring的配置文件 (springmvc.xml)
点击查看代码
<?xml version="1.0" encoding="UTF-8"?>

<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        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
		">
<!--开启mvc-->
    <mvc:annotation-driven />
<!--开启spring注解依赖-->
    <context:annotation-config></context:annotation-config>
    <!--自动扫描包-->
    <context:component-scan base-package="com.bh"></context:component-scan>

</beans>
3, 实现业务的的Controller类中(当然这个类一定是让spring容器管理的,也就是类名上面要追加@Controller), 在具体的某个方法上面通过 @RequestMapping(value=url) 来实现url的映射
点击查看代码
package com.bh.controller;

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

@Controller
public class HelloController {
@RequestMapping("/hello.do")//请求地址
//    @RequestMapping(value = "/hello.do", method = RequestMethod.GET)//method = RequestMethod.GET只接受get提交
//    @GetMapping(value = "/hello.do")//可替代@RequestMapping(value = "/hello.do", method = RequestMethod.GET)
//    @PostMapping(value = "/hello.do")//可替代@RequestMapping(value = "/hello.do", method = RequestMethod.POST)
    public String sayHello(){
        System.out.println("hello world");
        //请求转发
        //String str = "/result.html";

    //重定向
    String str = "redirect:/result.html";
        return str;
    }
}

posted @ 2023-06-03 14:43  liangkuan  阅读(21)  评论(0)    收藏  举报