SpringMVC基于非注解方式(SimpleUrlHandlerMapping)实现用户数据查询备忘(Intellij idea)

效果

这里写图片描述
这里写图片描述
—————点击查询用户后:
这里写图片描述
这里写图片描述


环境搭建及依赖

这里使用的处理器映射器是SimpleUrlHandlerMapping,对应的处理器适配器是HttpRequestHandlerAdapter

依赖引入

SpringMVC依赖:
    spring-web
    spring-webmvc
Servlet依赖:
    javax.servlet-api
JSTL依赖(JSP标准标签库,这里我们需要用到一些基本的JSP标签)
    jstl
    standard

配置文件

springmvc.xml       springmvc的配置文件
web.xml             web项目的配置文件
pom.xml             基于maven搭建项目的组织文件

java实现类

UserController.java   (需要实现HttpRequestHandler接口)    
User                  (实体类,用于表示一个用户数据)

jsp文件

index.jsp   首页,有查询用户的按钮
user.jsp    展示查询用户的数据

文件目录结构

这里写图片描述


整体过程

理解整体过程,重点是需要理解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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com</groupId>
    <artifactId>mvc_usertable</artifactId>
    <version>1.0.0</version>

    <packaging>war</packaging>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.14.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.14.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/jstl/jstl -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/taglibs/standard -->
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
    </dependencies>
</project>

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_3_1.xsd"
         version="3.1">
    <!--主页-->
    <welcome-file-list>
        <welcome-file>/pages/index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--配置文件位置-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

springmvc.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--注册controller-->
    <bean id="userController" class="com.zsb.controller.UserController"></bean>
    <!--配置处理器映射器-->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <!--将getUserList.do请求映射到上面注册的controller-->
                <prop key="/getUserList.do">userController</prop>
            </props>
        </property>
    </bean>
    <!--配置处理器适配器-->
    <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>
    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</beans>

UserController.java

import com.zsb.entity.User;
import org.springframework.web.HttpRequestHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class UserController implements HttpRequestHandler{
    public void handleRequest(HttpServletRequest Request, HttpServletResponse Response) 
        throws ServletException, IOException {
            List<User> userList = new ArrayList<User>();
            User user1 = new User(1,"TOM",25,"male");
            User user2 = new User(2,"Marry",20,"female");
            User user3 = new User(3,"peter",30,"male");
            userList.add(user1);
            userList.add(user2);
            userList.add(user3);
            //将用户信息放入Request对象里
            Request.setAttribute("userList",userList);   
            //当跳转到user.jsp时,当前请求数据仍然可以被访问到      
            Request.getRequestDispatcher("/pages/user.jsp").forward(Request,Response);
    }
}

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = 
        request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
    <title>主页</title>
    <base href="<%=basePath%>">
</head>
<body>
<h1>主页</h1>
欢迎进入主页!
<a href="<%=path%>/getUserList.do"><button>查询用户</button></a>
</body>
</html>

user.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
    <base href="<%=basePath%>">
    <title>用户数据页面</title>
</head>
<body>
<h1>用户数据页面</h1>
    <table border="1">
        <tr>
            <th>序号</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
        </tr>
        <c:if test = "${userList!=null}">
            <c:forEach items = "${userList}" var = "user">
                <tr>
                    <td>${user.userId}</td>
                    <td>${user.userName}</td>
                    <td>${user.age}</td>
                    <td>${user.sex}</td>
                </tr>
            </c:forEach>
        </c:if>
    </table>
</body>
</html>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值