复习第二点-2.基于注解的helloworld

web.xml
<?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>springmvc</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>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
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
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
>
    <!--
        这个标签仅是@Controller @RequestMapping 基本功能的注解可以使用
    -->
    <context:component-scan base-package="controller"/>
    <!--
            增加了这个标签后,提供了扩展功能
            它的处理类 AnnotationDrivenBeanDefinitionParser 会注册很多基于注解开发时所用到的
        Bean 对 象 到 容 器 中 。 其 中 包 含 RequestMappingHandlerMapping 、
        RequestMappingHandlerAdapter 与 ExceptionHandlerExceptionResolver 三个 bean。
    -->
    <mvc:annotation-driven/>

</beans>
AnnoController.java
package controller;

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

@Controller
//http://localhost:8888/springmvc_war_exploded/suibian/ann
// @RequestMapping 用于将一个 URI 绑定到类上或类的方法中。
@RequestMapping("/suibian")
public class AnnoController {
    //http://localhost:8888/springmvc_war_exploded/ann
    @RequestMapping("/ann")
    public ModelAndView annDemo(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/index.jsp");
        modelAndView.addObject("msg","Hello Oldlu");
        return modelAndView;
    }
}
展示效果

posted @ 2023-01-13 14:42  jsqup  阅读(8)  评论(0编辑  收藏  举报