0.Servlet回顾与SpringMvc快速搭建

1.使用idea快速搭建Web项目

1-1.创建项目

File--->new Project--->Java Enterprise--->Web Application

1-2.导入 web项目需要的 jar包(tomcat提供)

file--->project sturcture--->Modules---+号 jars or directories ---> 寻找 tomcat下 lib里的 ServletAPi和JSP api 勾选 点击apply ok

1-3.简单写一个Servlet确定能被访问到

package com.edu.web;
​
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
​
public class HelloServlet extends HttpServlet {
​
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
​
    }
​
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("hello world");
    }
}

并在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>HelloServlet</servlet-name>
        <servlet-class>com.edu.web.HelloServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <!--  请求路径  -->
        <url-pattern>/login</url-pattern>
    </servlet-mapping>
</web-app>

1-4.tomcat配置

  1. 选择 运行标记前的 Add Configuration

  2. 选择+ 搜索tomcat 选择local

  3. 选择的右边的 configure 去配置本地tomcat的路径.

  4. 选择 deployment部署项目

 

2.使用idea快速搭建SpringMVC项目(MAVEN项目)

2.1 创建 一个 Maven项目 不需要使用任何模版

2.2 右键项目名 --- add framework support(增加框架支持)-----web applicationContext

2.3导入相关的 jar包 配置 pom.xml

  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.13.RELEASE</version>
        </dependency>
    </dependencies>

2.4 在resources下 创建MVC的关于Spring的配置

<?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 https://www.springframework.org/schema/context/spring-context.xsd">
​
    <context:component-scan base-package="com.edu"></context:component-scan>

</beans>

 

2.5在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>
        <!--
           初始化 spring容器的配置文件地址
         -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <!--
           确保DispatcherServlet会随着 tomcat的开启而自动创建
         -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!--
            /  表示拦截除了jsp以为的所有请求
            /* 表示拦截所有包括jsp 相对上面效率会低一些
            /*.do  *.action 表示以action或者do结尾的 url会被 前端控制器拦截
         -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

 

2.6 创建UserController类

//确保当前的 控制器 交由Spring管理
@Controller
public class UserController {
​
    // 配置 请求的 路径映射
    @RequestMapping("/login")
    public String login(User user){
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
       //进行页面跳转 if("tom".equals(user.getUsername())&&"123456".equals(user.getPassword())){
            return "success.jsp";
        }else{
            return "redirect:fail.jsp";
        }
    }
}

 

2.7 配置maven的jar包 导入到 web-inf下的 lib中去

file--project structure --artifacts--web-inf新建lib文件夹 选中右边的 所有jar包 选择 put into web-inf/lib

2.8启动tomcat通过url 以表单或者是 浏览器地址栏直接访问这个Controller

阅读剩余
THE END