• Java -- SpringMVC学习笔记1、第一个SpringMvc程序


    1、第一个的注解的SpringMVC程序

    完成功能:用户填写表单提交一个请求,服务端处理器在接收到这个请求后,给出一条欢迎信息,在响应页面中显示该信息。

    • 添加依赖和插件、如下:
    <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
          <scope>provided</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.2.5.RELEASE</version>
        </dependency>
      </dependencies>
    
      <build>
        <plugins>
          <!-- 编码和编译和JDK版本 -->
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
        </plugins>
    </build>
    
    • 在web.xml文件中添加中央调度器:
    <servlet>
        <!--中央调度器-->
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!--spring配置文件 -->
            <param-value>classpath:dispatcherServlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    
    • 创建处理器:
    /**
     * Controller:表示当前类为处理器
     */
    @Controller
    public class LoginController
    {
        /**
         * RequestMapping:表示当前方法为处理器方法。该方法要对 value 属性所指定的 URI
         * 进行处理与响应。被注解的方法的方法名可以随意
         * @param userName 接收参数
         * @return 返回值、里边有值和视图路径
         */
        @RequestMapping(value = "/login.do", method = RequestMethod.POST)
        public ModelAndView loginCheck(String userName)
        {
            ModelAndView mv = new ModelAndView();
            mv.addObject("name", userName);
            mv.setViewName("/welcome.jsp");
            return mv;
        }
    }
    
    • 在resources目录下创建spring配置文件、添加组件扫描器:
    <!--声明组件扫描器-->
    <context:component-scan base-package="com.rg.controllers"/>
    
    • 创建index.jsp:
    <form method="post" action="login.do">
        UserName:<input type="text" name="userName">
        <input type="submit" value="提交">
    </form>
    
    • 创建welcome.jsp:
    <body>
    欢迎${name}使用SpringMvc框架
    </body>
    

    这样、一个简单SpringMvc框架就搭建好了、用户在index页面填写表单信息、中央调度器转发请求、处理器处理请求、最后返回到指定视图展示欢迎信息。

  • 相关阅读:
    php类自动加载
    tp5自定义分页参数
    cURL error 60: SSL certificate problem...
    ajax动态刷新的元素,导致绑定事件失效
    thinkphp5省市区三级联动例子
    restful状态码常用
    mysql的like子句
    mysql官方的测试数据库employees超30万的数据,安装方法介绍
    Aes加解密,php
    php5.6,Ajax报错,Warning: Cannot modify header information
  • 原文地址:https://www.cnblogs.com/dcy521/p/14791685.html
Copyright © 2020-2023  润新知