springMVC的数据封装
编写实体类:
package cn.mepu.domain;
/**
* @User 艾康
* @create 2019-11-12 13:56
*/
public class User {
private String username;
private String sex;
private long phone;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public long getPhone() {
return phone;
}
public void setPhone(long phone) {
this.phone = phone;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", sex='" + sex + '\'' +
", phone=" + phone +
'}';
}
}
控制器:
package cn.mepu.controller;
import cn.mepu.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @User 艾康
* @create 2019-11-12 10:24
* 控制器
*/
@Controller
public class HelloController {
//url请求时执行该方法 method:请求方式 params该配置表示必须传入username参数 headers请求头必须包含的请求头
@RequestMapping("/hello")
public String sayHello(User user){
System.out.println(user);
return "success";
}
}
配置中文乱码问题;
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 配置前端访问控制器-->
<servlet>
<servlet-name>dispatcherServlet</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>
<!-- 配置启动时加载对象-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置中文乱码过滤器-->
<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 version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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
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">
<!--配置spring启动时扫描的包-->
<context:component-scan base-package="cn.mepu"></context:component-scan>
<!--配置试图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 跳转文件路径-->
<property name="prefix" value="\WEB-INF\pages\"></property>
<!-- 跳转文件后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
<!--开启注解-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
页面:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/11/12
Time: 9:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>入门案例</title>
</head>
<body>
<form action="hello" method="post">
姓名:<input type="text" name="username"><br/>
性别:<input type="text" name="sex"><br/>
电话:<input type="text" name="phone"><br/>
<input type="submit" value="登录">
</form>
</body>
</html>
springMVC的数据封装的更多相关文章
- springMVC数据封装成POJO
springMVC把前台的数据封装为POJO与struts2的封装形式不同.struts2需要在控制器声明需封装的POJO,而springMVC不需要任何准备工作,只需在相应的方法的参数中加上需封装的 ...
- SpringMVC将通过ajax发送的 json数据封装成JavaBean
SpringMVC将通过ajax发送的 json数据封装成JavaBean 通过ajax发送的 json数据封装成JavaBean对发送时有如下要求: 1.发送的数据类型必须时UTF-8 2.发送的必 ...
- springMVC form表单提交多个对象集合--使用ajax提交--前台json格式数据封装方法
(function ($) { $.fn.serializeJson = function () { var jsonData1 = {}; var serializeArray = this.ser ...
- SpringMVC拦截器
springmvc的拦截器 需求:进行用户的访问控制,判断用户是否登陆,如果登陆进行正常访问,如果没有登陆跳转到登陆页面. 1自定义拦截器类 package org.guangsoft.utils; ...
- (spring-第21回【MVC基础篇】)SpringMVC一点就通
概述 Spring MVC通过一套MVC注解,让POJO变成处理请求的控制器,无需实现任何接口,同时,SpringMVC还支持REST风格的URL请求:注解驱动和REST风格的Spring MVC是S ...
- springMVC 注解版
http://blog.csdn.net/liuxiit/article/details/5756115 http://blog.csdn.net/hantiannan/article/categor ...
- SpringMVC第二篇【过滤编码器、注解开发、requestMapping、业务方法与传统参数】
SpringMVC过滤编码器 在SpringMVC的控制器中,如果没有对编码进行任何的操作,那么获取到的中文数据是乱码! 即使我们在handle()方法中,使用request对象设置编码也不行!原因也 ...
- SpringMVC第一篇【介绍、入门、工作流程、控制器】
什么是SpringMVC? SpringMVC是Spring家族的一员,Spring是将现在开发中流行的组件进行组合而成的一个框架!它用在基于MVC的表现层开发,类似于struts2框架 为什么要使用 ...
- SpringMVC的一点理解
1.MVC(Model-View-Controller) 用慕课网上的一个图来看看MVC Front Controller(前端控制器):把客户的请求分发给不同的控制器去生成业务数据,将生成的业务数据 ...
随机推荐
- leetcode-7-整数翻转
问题: package com.example.demo; public class Test7 { /** * 整数翻转 123,-123,120等数字 * 思路: * 1.获取原始数字的%10的余 ...
- Linux系统安全
简单优化: 1.删除不必要的软件包(如postfix等) yum remove -y postfix 安装管理:1.口令 1.1至少8个字符,大小写.特殊字符和数字组合,定期更改 1.2口令长度可以编 ...
- mysql中关于--login-path使用
在控制台登陆数据库,快捷登录 在控制台连接数据库,需要每次输入账号密码,感觉很麻烦,偶然发现可以通过login-path保存信息,实现快捷登录,这里记录下. 保存账号信息 mysql_config_e ...
- linux100day(day7)--用户管理和权限管理简单介绍
系统基础 计算机的三大部件 CPU 内存 IO 总线 一般使用system call和api来调用硬件 一些基础命令, pwd 查看当前路径 cal 计算器 clock 时钟 hwclock 显示与设 ...
- linux随笔-03
必须掌握的Linux命令 系统状态检测命令 1.ifconfig命令 ifconfig命令用于获取网卡配置与网络状态等信息,格式为“ifconfig [网络设备] [参数]”. 使用ifconfig命 ...
- plsql exception
EXCEPTION aligns with BEGIN ... END blocks. There is no BEGIN inside your loop, so there should be n ...
- EnumPrinters
https://blog.csdn.net/jilong17/article/details/6663734 已解决.开始hook的spoolss.dll里面的EnumPrinterW函数,应该hoo ...
- WebRequest发送请求并接收返回值
public string getXmlStr(string hphmcode) { string Url = "http://localhost:80 ...
- linux下关闭Oracle及关机
参考了如下文章 linux环境下连接oracle 并操作oracle数据库 1 先看lsnrctl和oracle service是否都启动了.如果没有启动,先启动:如net start,启动所有服务. ...
- 1、jQuery操作Dom
1.添加元素 <code> <script language="JavaScript">$().ready(function(){$("input ...