springmvc学习笔记(七):在控制器的 业务方法中,通过普通参数 接收表单提交的数据(注解实现)


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

    <!-- 配置 springmvc 核心控制器 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <!-- 配置初始化参数:到 src 目录下加载 springmvc.xml 配置文件 -->
        <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>*.action</url-pattern>
    </servlet-mapping>

    <!-- 注册 spring 核心编码过滤器,指定中文编码,防止中文乱码 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-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:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- 开启注解扫描:扫描指定包下的所有注解;需要引入 context 名称空间 -->
    <context:component-scan base-package="com.springmvc.demo"/>

</beans>

emp.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    员工注册:<br>
    <form action="${pageContext.request.contextPath}/emp/register.action" method="post">
        姓名:<input type="text" name="username"><br>
        月薪:<input type="text" name="salary"><br>
        <input type="submit" value="注册">
    </form>

    <hr>

    员工登录:<br>
    <form action="${pageContext.request.contextPath}/emp/login.action" method="post">
        姓名:<input type="text" name="username"><br>
        <input type="submit" value="登录">
    </form>
</body>
</html>

EmpAction.java:

package com.springmvc.demo;

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

/**
 * 控制器:单例
 *  @Controller 注解:表示当前类是一个控制器
 *  @RequestMapping(value = "/emp") 此注解放在类上面,表示模块的根路径
 */
@Controller
@RequestMapping(value = "/emp")
public class EmpAction {

    /**
     * 业务方法:员工注册
     *  @RequestMapping 注解:表示请求的映射,所有 /register.action 的请求,都会直接进入该方法;
     *  method = RequestMethod.POST:表示此方法只能接受 POST 请求;
     *
     *  获取表单提交的数据:
     *  表单提交数据时,会自动检查业务方法的参数中,是否存在和表单提交数据的 name 属性一样的名字,
     *  如果存在,则直接把表单提交的数据传入该参数;
     *  比如:将表单提交的 姓名数据 传给 username 参数;将表单提交的 月薪参数 传给 salary 参数;
     */
    @RequestMapping(value = "/register.action", method = RequestMethod.POST)
    public String register(Model model, String username, String salary) throws Exception{
        System.out.println("员工注册-->" + username + ":" + salary);
        // 封装数据
        model.addAttribute("message", "员工注册成功!");
        // 返回视图的真实路径
        return "/index.jsp";
    }

    /**
     * 业务方法:员工登录
     *  @RequestMapping 注解:表示请求的映射,所有 /login.action 的请求,都会直接进入该方法;
     *  method = {RequestMethod.GET, RequestMethod.POST}:表示此方法既能接收 POST 请求,也能接收 GET 请求;
     *  如果不写 method,默认就是既支持 POST 请求,也支持 GET 请求;
     */
    @RequestMapping(value = "/login.action", method = {RequestMethod.GET, RequestMethod.POST})
    public String login(Model model, String username) throws Exception{
        System.out.println("员工登录-->" + username);
        // 封装数据
        model.addAttribute("message", "员工登录成功!");
        // 返回视图的真实路径
        return "/index.jsp";
    }

}

index.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  index.jsp<br>
  <%-- 获取控制器传到 jsp 的数据 --%>
  ${requestScope.message}
  </body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值