SringMVC非注解方式

SringMVC(非注解方式)

模拟MVC框架

配置pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>day36springMvc</artifactId>
    <version>1.0-SNAPSHOT</version>
 

    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

   <dependencies>
       <dependency>
           <groupId>org.projectlombok</groupId>
           <artifactId>lombok</artifactId>
           <version>1.18.16</version>
       </dependency>

       <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>javax.servlet-api</artifactId>
           <version>3.1.0</version>
           <scope>provided</scope>
       </dependency>

       <dependency>
           <groupId>jstl</groupId>
           <artifactId>jstl</artifactId>
           <version>1.2</version>
       </dependency>

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

<!--    <build>-->
<!--        <plugins>-->
<!--            &lt;!&ndash; define the project compile level &ndash;&gt;-->
<!--            <plugin>-->
<!--                <groupId>org.apache.maven.plugins</groupId>-->
<!--                <artifactId>maven-compiler-plugin</artifactId>-->
<!--                <version>3.6.1</version>-->
<!--                <configuration>-->
<!--                    <source>1.8</source>-->
<!--                    <target>1.8</target>-->
<!--                </configuration>-->
<!--            </plugin>-->

            <!-- 添加tomcat插件 -->
<!--            <plugin>-->
<!--                <groupId>org.apache.tomcat.maven</groupId>-->
<!--                <artifactId>tomcat7-maven-plugin</artifactId>-->
<!--                <version>2.2</version>-->
<!--                <configuration>-->
<!--                    <path>/</path>-->
<!--                    <port>8081</port>-->
<!--                </configuration>-->
<!--            </plugin>-->
<!--        </plugins>-->
<!--    </build>-->


</project>

在这里插入图片描述

创建实体类

package com.pojo;

import lombok.Data;

@Data
public class Product {
    private int pid;
    private String pname;
    private double price;
}

index.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>index</title>
</head>
<body>
    <h1>this is index page.</h1>
    <a href="ProductInputMVCServlet">save product</a>
<%--    <a href="ProductInputServlet">save product</a>--%>
</body>
</html>

原始Servlet

package com.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ProductInputServlet",value = "/ProductInputServlet")
public class ProductInputServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getRequestDispatcher("WEB-INF/view/saveProduct.jsp").forward(request,response);
    }
}
package com.controller;

import com.pojo.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ProductSaveServlet",value = "/ProductSaveServlet")
public class ProductSaveServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //获取参数
        String spid = request.getParameter("pid");
        int pid = spid == null ? 0: Integer.parseInt(spid);
        String pname = request.getParameter("pname");
        String sprice = request.getParameter("price");
        double price = sprice == null ? 0 : Double.parseDouble(sprice);

        Product p = new Product();

        p.setPid(pid);
        p.setPname(pname);
        p.setPrice(price);

        request.setAttribute("p", p);

        request.getRequestDispatcher("WEB-INF/view/detailProduct.jsp").forward(request, response);

    }
}

保存页面saveProduct.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>this is product save page.</h1>
<ul>
<c:forEach items="${errors}" var="e">
    <li><font color="red">${e}</font></li>
</c:forEach>
</ul>
<form action="ProductSaveMVCServlet" method="post">
<%--<form action="ProductSaveServlet" method="post">--%>
  pid: <input type="text" name="pid" /><p />
  pname: <input type="text" name="pname" /><p />
  price: <input type="text" name="price" /><p />
   <input type="submit" value="save" /><p />
</form>
</body>
</html>

详情页面detailProduct.jsp

<%--
  Created by IntelliJ IDEA.
  User: mac
  Date: 2022/9/20
  Time: 14:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>this is product detail page.</h1>
${p.pid}
<hr />

${p.pname}
<hr />

${p.price}
<hr />

</body>
</html>

模拟MVC

Controller.java接口

package com.controller.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public interface Controller {
    String handlerRequest(HttpServletRequest request, HttpServletResponse response);
}

两个接口实现类

package com.controller.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ProductInputController implements Controller{
    @Override
    public String handlerRequest(HttpServletRequest request, HttpServletResponse response) {
        return "WEB-INF/view/saveProduct.jsp";
    }
}
package com.controller.mvc;

import com.pojo.Product;
import com.validate.ProductValidate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class ProductSaveController implements Controller{
    @Override
    public String handlerRequest(HttpServletRequest request, HttpServletResponse response) {
        String spid = request.getParameter("pid");
        int pid = spid == null ? 0: Integer.parseInt(spid);
        String pname = request.getParameter("pname");
        String sprice = request.getParameter("price");
        double price = sprice == null ? 0 : Double.parseDouble(sprice);

        Product p = new Product();

        p.setPid(pid);
        p.setPname(pname);
        p.setPrice(price);

        //数据校验
        ProductValidate pv = new ProductValidate();

        List<String> list = pv.validate(p);

        if(list.size() != 0){

            request.setAttribute("errors", list);

            return "WEB-INF/view/saveProduct.jsp";
        }



        request.setAttribute("p", p);

        return "WEB-INF/view/detailProduct.jsp";
    }
}

模拟前端控制器适配匹配

package com.controller.mvc;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "DispatcherServlet", value = {"/ProductInputMVCServlet", "/ProductSaveMVCServlet"})
public class DispatcherServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        System.out.println(uri);

        Controller contr = null;

        if("/ProductSaveMVCServlet".equals(uri)){
            //  product save
            contr = new ProductSaveController();
        }else if("/ProductInputMVCServlet".equals(uri)){
            // product input
            contr = new ProductInputController();
        }

        String path = contr.handlerRequest(request, response);

        request.getRequestDispatcher(path).forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

SpringMVC框架(非注解实现方式)

在这里插入图片描述

pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>day36springMvc</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>SpringMVC</artifactId>
     <packaging>war</packaging>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>

Product实体类

package com.pojo;

import lombok.Data;

@Data
public class Product {

    private String name;
    private String img;
    private double price;
}

ProductDetailController

package com.controller;

import com.pojo.Product;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ProductDetailController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String name = request.getParameter("name");
        String img = request.getParameter("img");
        String sprice = request.getParameter("price");

        double price = sprice == null ? 0 : Double.parseDouble(sprice);

        Product p = new Product();

        p.setName(name);
        p.setImg(img);
        p.setPrice(price);
        return new ModelAndView("detailProduct","p",p);
    }
}

ProductSaveController

package com.controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ProductSaveController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return new ModelAndView("saveProduct");
    }
}

mvc.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean name="/detailProduct" class="com.controller.ProductDetailController"></bean>
    <bean name="/saveProduct" class="com.controller.ProductSaveController"></bean>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

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>mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:mvc.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

saveProduct.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>this is product save page.</h1>

<form action="detailProduct" method="post">
  name: <input type="text" name="name" /><p />
  img: <input type="text" name="img" /><p />
  price: <input type="text" name="price" /><p />
   <input type="submit" value="save" /><p />
</form>
</body>
</html>

detailProduct.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>this is product detail page.</h1>
${p.name}
<hr />

${p.img}
<hr />

${p.price}
<hr />

</body>
</html>
ge.</h1>

<form action="detailProduct" method="post">
  name: <input type="text" name="name" /><p />
  img: <input type="text" name="img" /><p />
  price: <input type="text" name="price" /><p />
   <input type="submit" value="save" /><p />
</form>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值