• 注解的方式搭建springmvc步骤


    1.cope   jar包到lib中

    2.配置web.xml文件

    <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        </servlet>
    
        <!-- Map all requests to the DispatcherServlet for handling -->
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>

    3.在web-inf中创建springmvc的配置文件springmvc-servlet.xml

    分别配置注解驱动、扫描器、视图解析器

    <!-- springmvc注解驱动 -->
        <mvc:annotation-driven/>
        <!-- 扫描器 -->
        <context:component-scan base-package="com.tideway.springmvc"></context:component-scan>
        <!-- 配置视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/views/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>

    4.创建发送请求页面index.jsp

    <form action="hello.do" method="post">
            ID:<input type="text" name="userName"/>
            <input type="submit" value="submit"/>
        </form>

    5.创建控制器HelloWorld.java

    package com.tideway.springmvc;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class HelloWorld {
        
        @RequestMapping(value="/hello.do")
        public String hello(String userName,Model model){
            System.out.println(userName);
            model.addAttribute("helloworld", "Hello:"+userName);
            return "success";
        }
    }

    6.在WebContent文件夹下创建views文件夹下创建响应页面success.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <!-- el方式 -->
    <h1>${helloworld }</h1>
    </body>
    </html>
  • 相关阅读:
    nginx访问控制
    nginx的请求限制
    nginx目录及配置语法
    安装Nginx
    Docker Service启动时挂载docker命令
    禁止flyme自动下载rom
    docker.service 修改指南
    debian 10.x (buster) 离线安装docker及卸载
    按照容器名称清除docker容器产生的日志文件内容
    debian修改系统语言为英文
  • 原文地址:https://www.cnblogs.com/yunqing/p/6165481.html
Copyright © 2020-2023  润新知