天天看點

基于SSM架構的web入門項目(七)學習記錄7.SSM整合-客戶添加

配合哔哩哔哩視訊學習【SSM 架構】SpringMVC+Spring+Mybatis SSM 整合+實戰+源碼17-18集

7.SSM整合-客戶添加

7.1.在CustomerController裡面添加方法

package com.ssmlcx.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ssmlcx.domain.Customer;
import com.ssmlcx.service.CustomerService;

@Controller
@RequestMapping("/customer")
public class CustomerController {
	
	//注入業務對象
	@Resource
	private CustomerService customerService;
	
/*	@RequestMapping("/test")
	public String test()
	{
		return "test";
	}*/
	/**
	 * 跳轉到input.jsp
	 */
	@RequestMapping("/input")
	public String input()
	{
		return "input";
	}
	
	/**
	 * 儲存客戶
	 */
	@RequestMapping("/svae")
	public String save(Customer customer)
	{
		customerService.saveCustomer(customer);
		return "succ";
	}
}

           

7.2.編寫input.jsp錄入客戶頁面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>" target="_blank" rel="external nofollow" >
    
    <title>客戶錄入頁面</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css" target="_blank" rel="external nofollow" >
	-->

  </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="name"/><br/>
    	客戶手機:<input type="text" name="telephone"/><br/>
    	客戶姓名:<input type="text" name="name"/><br/>
    	客戶住址:<input type="text" name="address"/><br/>
    	<input type="submit" value="儲存">
    </form>
  </body>
</html>

           

這時發現頁面傳到Controller,中文資料就亂碼,這時可以在web.xml加多編碼過濾器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>01.mybatis</display-name>
  
  <!-- 配置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>/*</url-pattern>
  </filter-mapping>
  
  
  
  <!-- 啟動springMVC -->
  <servlet>
  	<servlet-name>DispatcherServlet</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- 參數:讀取 spring-mvc.xml-->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:spring-mvc.xml</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>DispatcherServlet</servlet-name>
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
  
  <!-- 啟動spring -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 修改路徑 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>
           
基于SSM架構的web入門項目(七)學習記錄7.SSM整合-客戶添加
基于SSM架構的web入門項目(七)學習記錄7.SSM整合-客戶添加

繼續閱讀