7. SSM 整合-客户添加
7.1.在 CustomerController 里面添加方法、
package cn.sm1234.controller;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.sm1234.domain.Customer;
import cn.sm1234.service.CustomerService;
@Controller
@RequestMapping("/customer")
public class CustomerController {
@Resource
private CustomerService customerService;
@RequestMapping("/input")
public String input(){
return "input";
}
@RequestMapping("/save")
public String save(Customer customer){
System.out.println("======"+customer);
customerService.saveCustomer(customer);
return "succ";
}
}
7.2.编写 input.jsp 录入客户页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>客户录入页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="${pageContext.request.contextPath}/customer/save.action" method="post">
客户姓名:<input type="text" name="name"/><br/>
客户性别:
<input type="radio" name="gender" value="男"/>男
<input type="radio" name="gender" value="女"/>女
<br/>
客户手机:<input type="text" name="telephone"/><br/>
客户住址:<input type="text" name="address"/><br/>
<input type="submit" value="保存">
</form>
</body>
</html>
这时发现页面传参到 Controller,中文数据乱码,这时可以在 web.xml 加多编码 过滤器:
<!-- 配置SpringMVC编码过滤器 -->
<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>