基於SSM框架的web入門項目(七)學習記錄

配合嗶哩嗶哩視頻學習【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%>">
    
    <title>客戶錄入頁面</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<!--
	<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="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>

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章