• SpringMvc入门案例 (SpringMvc 是单例的)


    1:导入相关的jar包

    2:在web.xml文件中配置 (srping mvc 配置的servlet)

        
        <!--加载资源的第一种方式-->
        <servlet>
            <servlet-name>springmvc1</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!--  加载Springmvc的配置文件 -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath*:springmvc.xml</param-value>
            </init-param>
        </servlet>
        <servlet-mapping>
            <servlet-name>springmvc1</servlet-name>
            <url-pattern>*.action</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:mvc="http://www.springframework.org/schema/mvc"
          xsi:schemaLocation="
          http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
          ">
          
        <!--控制器   尤为注意这里的name值是代表的请求路径。。。-->
        <bean  name="/hello.action" class="com.cn.controllers.HelloAction"></bean>
    
        <!--映射器 beanNameUrl. 这个是默认的配置 意思就是那bean的name作为url访问路径
            可选
        -->
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
    
        <!--适配器 这个也是默认的配置 表示会去寻找实现了Controller的java类
            可选
        -->
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
    
        <!--试图解析器
            可选
        -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    </beans>    

    4:编写一个Controller控制器类(一个普通的java类实现Controller接口)

    public class HelloAction implements Controller {
    
        @Override
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("message","springmvc_test");
            //modelAndView.setViewName("/success.jsp");
           modelAndView.setViewName("success");
         return modelAndView; } 
      }

    启动tomcat测试访问:
    http://localhost:8080/hello.action

    Srpingmbc的大概流程图

    坚持
  • 相关阅读:
    TCP通过哪些措施,保证传输可靠
    http协议---简述
    单播、广播、组播、多播
    axios 设置超时时间 timeout
    axios 处理并发请求
    vue 模板语法
    vue keep-alive
    v-if 条件渲染分组
    debounce 防抖动函数
    vue scoped 深度作用选择器
  • 原文地址:https://www.cnblogs.com/gaoSJ/p/13024098.html
Copyright © 2020-2023  润新知