SpringMVC: @RequestParam注解进行简单数据绑定实例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38737992/article/details/88087163

1.  项目目录

项目目录如下图所示。

2.  源文件编写

2.1  最主要的Controller编写

HelloUserController.java :

package com.study.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

// @Controller 标识是一个控制器
@Controller
public class HelloUserController {

    // 访问地址:http://localhost:8080/helloUser.form
    @RequestMapping(value = "/helloUser")

    // 将请求中的 username 参数的值赋给形参 userName,
    // 如果请求中 没有username 参数,那么 userName 默认为 “未传入username参数”。
    // defaultValue = "未传入username参数" 设置默认值
    public String handleRequest(Model model, @RequestParam(value = "username",
            defaultValue = "未传入username参数") String userName) throws Exception {

        // 向 模型 中添加数据
        model.addAttribute("message", userName);

        return "helloUser";
    }
}

test.java:

package com.study.springmvc;

public class test {
    // empty
}

2.2  xml 文件编写

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">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- SpringMVC 的前端控制器 -->
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- 拦截了以 ".form" 结尾的URL请求,Web 应用接收到该类请求之后 , 会调用前端控制器-->
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.form</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

</web-app>

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

  <!-- 配置视图解析器 -->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <!-- 前缀 -->
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <!-- 后缀 -->
    <property name="suffix" value=".jsp"></property>
  </bean>

  <!-- 扫描配置 -->
  <context:component-scan base-package="com.study.springmvc.controller"></context:component-scan>

</beans>

applicationContext.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">

  <!-- empty -->
</beans>

2.3  jsp 文件编写

<%--
  Created by IntelliJ IDEA.
  User: Yimso
  Date: 2019/3/2
  Time: 10:48
  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>${message}</h1>
</body>
</html>

3.  项目测试

3.1 未传入 username 参数

访问 http://localhost:8080/helloUser.form , 结果如图所示:

3.2 传入username 参数

访问  http://localhost:8080/helloUser.form?username="简单数据绑定",结果如图所示:

END。

猜你喜欢

转载自blog.csdn.net/qq_38737992/article/details/88087163
今日推荐