在“易居住房4”的基础上进行增加或修改代码
“IHouseViewDao.java”增加代码
void postHouseInter(HouseInter houseInter);
“IHouseViewService.java”增加代码
void postHouseInter(HouseInter houseInter);
“HouseViewService.java”增加代码
@Override
public void postHouseInter(HouseInter houseInter) {
houseViewDao.postHouseInter(houseInter);
}
“HouseViewController.java”
在@Param(value = "houseImg6") MultipartFile houseImg6
后加HttpSession session
修改代码
将
Map<String, MultipartFile> images=new HashMap<>();
改为
Map<String, byte[]> images=new HashMap<>();
在if语句的picName,houseHeadimg
后加.getBytes()
,以此类推
将
Set<String> keys=images.keySet();
for (String key:keys){
MultipartFile file=images.get(key);
YijuUtil.upload(file.getBytes(),key);
}
houseViewService.postHouse(house);
改为
session.setAttribute("house",house);
session.setAttribute("images",images);
将
houseInfo.setHouseId(10001);
houseViewService.postHouseInfo(houseInfo);
改为
session.setAttribute("houseInfo",houseInfo);
并增加代码
@RequestMapping("responsePost3.do")
@ResponseBody
public String responsePost3(HouseInter houseInter,HttpSession session){
/*houseInter.setInterId(10013);
houseViewService.postHouseInter(houseInter);*/
session.setAttribute("houseInter",houseInter);
return new JSONObject().toString();
}
@RequestMapping("doHousePost.do")
public String doHousePost(HttpSession session) throws IOException {
House house= (House) session.getAttribute("house");
house.setUserId(10001);
Map<String,byte[]> images= (Map<String, byte[]>) session.getAttribute("images");
HouseInfo houseInfo= (HouseInfo) session.getAttribute("houseInfo");
HouseInter houseInter= (HouseInter) session.getAttribute("houseInter");
houseViewService.postHouse(house);
Set<String> keys=images.keySet();
for (String key:keys){
byte[] bytes=images.get(key);
YijuUtil.upload(bytes,key);
}
houseInfo.setHouseId(house.getHouseId());
houseViewService.postHouseInfo(houseInfo);
houseInter.setHouseId(house.getHouseId());
houseViewService.postHouseInter(houseInter);
return "redirect:findHouseById.do?houseId="+house.getHouseId();
}
“HouseViewController.java”完整代码如下
package com.yiju.controller;
import com.github.pagehelper.PageInfo;
import com.yiju.bean.House;
import com.yiju.bean.HouseInfo;
import com.yiju.bean.HouseInter;
import com.yiju.pojo.HouseView;
import com.yiju.service.IHouseViewService;
import com.yiju.util.YijuUtil;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Controller
@RequestMapping("house")
public class HouseViewController {
@Autowired
private IHouseViewService houseViewService;
@RequestMapping("findFourHouse.do")
public ModelAndView findFourHouse(){
ModelAndView mv=new ModelAndView();
List<HouseView> newHouses=houseViewService.findFourHouseByType(0);
List<HouseView> oldHouses=houseViewService.findFourHouseByType(1);
List<HouseView> rentHouses=houseViewService.findFourHouseByType(2);
mv.addObject("newHouses",newHouses);
mv.addObject("oldHouses",oldHouses);
mv.addObject("rentHouses",rentHouses);
mv.setViewName("../main");
return mv;
}
@RequestMapping("findHouseById.do")
public ModelAndView findHouseByHouseId(int houseId){
HouseView house=houseViewService.findHouseByHouseId(houseId);
ModelAndView mv=new ModelAndView();
mv.addObject("houseInfo",house);
mv.setViewName("details");
return mv;
}
@RequestMapping("findHouse.do")
public ModelAndView findHouse(int currentPage,int houseType){
ModelAndView mv=new ModelAndView();
List<HouseView> houses=houseViewService.findHouseByType(currentPage,houseType);
List<HouseView> fh=houseViewService.findFourHouseByType(houseType);
PageInfo<HouseView> pageInfo=new PageInfo<>(houses);
mv.addObject("pageInfo",pageInfo);
mv.addObject("fh",fh);
if (houseType==0){
mv.setViewName("newhouse");
}else if (houseType==1){
mv.setViewName("oldhouse");
}else {
mv.setViewName("renthouse");
}
return mv;
}
@RequestMapping("toHousePost2.do")
public ModelAndView toHousePost2(@Param(value = "houseTitle") String houseTitle,
@Param(value = "houseAddress") String houseAddress,
@Param(value = "housePrice") BigDecimal housePrice,
@Param(value = "priceUnit") String priceUnit,
@Param(value = "houseHeadimg") MultipartFile houseHeadimg,
@Param(value = "housePlanimg1") MultipartFile housePlanimg1,
@Param(value = "housePlanimg2") MultipartFile housePlanimg2,
@Param(value = "houseImg1") MultipartFile houseImg1,
@Param(value = "houseImg2") MultipartFile houseImg2,
@Param(value = "houseImg3") MultipartFile houseImg3,
@Param(value = "houseImg4") MultipartFile houseImg4,
@Param(value = "houseImg5") MultipartFile houseImg5,
@Param(value = "houseImg6") MultipartFile houseImg6,
HttpSession session) throws IOException {
House house=new House();
house.setHouseTitle(houseTitle);
house.setHouseAddress(houseAddress);
house.setHousePrice(housePrice);
house.setPriceUnit(priceUnit);
house.setUserId(10007);
Map<String, byte[]> images=new HashMap<>();
if (houseHeadimg.getSize()!=0){
String picName= YijuUtil.getPicName();
house.setHouseHeadimg(picName);
images.put(picName,houseHeadimg.getBytes());
}
if (housePlanimg1.getSize()!=0){
String picName= YijuUtil.getPicName();
house.setHouseHeadimg(picName);
images.put(picName,housePlanimg1.getBytes());
}
if (housePlanimg2.getSize()!=0){
String picName= YijuUtil.getPicName();
house.setHouseHeadimg(picName);
images.put(picName,housePlanimg2.getBytes());
}
if (houseImg1.getSize()!=0){
String picName= YijuUtil.getPicName();
house.setHouseHeadimg(picName);
images.put(picName,houseImg1.getBytes());
}
if (houseImg2.getSize()!=0){
String picName= YijuUtil.getPicName();
house.setHouseHeadimg(picName);
images.put(picName,houseImg2.getBytes());
}
if (houseImg3.getSize()!=0){
String picName= YijuUtil.getPicName();
house.setHouseHeadimg(picName);
images.put(picName,houseImg3.getBytes());
}
if (houseImg4.getSize()!=0){
String picName= YijuUtil.getPicName();
house.setHouseHeadimg(picName);
images.put(picName,houseImg4.getBytes());
}
if (houseImg5.getSize()!=0){
String picName= YijuUtil.getPicName();
house.setHouseHeadimg(picName);
images.put(picName,houseImg5.getBytes());
}
if (houseImg6.getSize()!=0){
String picName= YijuUtil.getPicName();
house.setHouseHeadimg(picName);
images.put(picName,houseImg6.getBytes());
}
/*Set<String> keys=images.keySet();
for (String key:keys){
byte[] bytes=images.get(key);
YijuUtil.upload(bytes,key);
}*/
/*houseViewService.postHouse(house);*/
session.setAttribute("house",house);
session.setAttribute("images",images);
ModelAndView mv=new ModelAndView();
mv.setViewName("housePost2");
return mv;
}
@RequestMapping("responsePost2.do")
@ResponseBody
public String responsePost2(HouseInfo houseInfo,HttpSession session){
/*houseInfo.setHouseId(10001);
houseViewService.postHouseInfo(houseInfo);*/
session.setAttribute("houseInfo",houseInfo);
return new JSONObject().toString();
}
@RequestMapping("responsePost3.do")
@ResponseBody
public String responsePost3(HouseInter houseInter,HttpSession session){
/*houseInter.setInterId(10013);
houseViewService.postHouseInter(houseInter);*/
session.setAttribute("houseInter",houseInter);
return new JSONObject().toString();
}
@RequestMapping("doHousePost.do")
public String doHousePost(HttpSession session) throws IOException {
House house= (House) session.getAttribute("house");
house.setUserId(10001);
Map<String,byte[]> images= (Map<String, byte[]>) session.getAttribute("images");
HouseInfo houseInfo= (HouseInfo) session.getAttribute("houseInfo");
HouseInter houseInter= (HouseInter) session.getAttribute("houseInter");
houseViewService.postHouse(house);
Set<String> keys=images.keySet();
for (String key:keys){
byte[] bytes=images.get(key);
YijuUtil.upload(bytes,key);
}
houseInfo.setHouseId(house.getHouseId());
houseViewService.postHouseInfo(houseInfo);
houseInter.setHouseId(house.getHouseId());
houseViewService.postHouseInter(houseInter);
return "redirect:findHouseById.do?houseId="+house.getHouseId();
}
}
“HouseMapper.xml”增加代码
<insert id="postHouseInter" parameterType="com.yiju.bean.HouseInter" useGeneratedKeys="true" keyProperty="interId">
insert into tb_house_inter(house_id, bed, washing, air, balcony, ice, toilet, kitchen, tv, heater, wardrobe, heating, internet, sofa, create_time, update_time)
values (#{houseId}, #{bed}, #{washing}, #{air}, #{balcony}, #{ice}, #{toilet}, #{kitchen}, #{tv}, #{heater}, #{wardrobe}, #{heating}, #{internet}, #{sofa}, #{createTime}, #{updateTime})
</insert>
“housePost3.jsp”在</body>
后增加代码
<script type="text/javascript">
var bed = 0;
var washing = 0;
var air = 0;
var balcony = 0;
var ice = 0;
var toilet = 0;
var kitchen = 0;
var tv = 0;
var heater = 0;
var wardrobe = 0;
var heating = 0;
var internet = 0;
var sofa = 0;
function bedClick() {
if (bed == 0) bed = 1;
else bed = 0;
}
function washingClick() {
if (washing == 0) washing = 1;
else washing = 0;
}
function airClick() {
if (air == 0) air = 1;
else air = 0;
}
function balconyClick() {
if (balcony == 0) balcony = 1;
else balcony = 0;
}
function iceClick() {
if (ice == 0) ice = 1;
else ice = 0;
}
function toiletClick() {
if (toilet == 0) toilet = 1;
else toilet = 0;
}
function kitchenClick() {
if (kitchen == 0) kitchen = 1;
else kitchen = 0;
}
function tvClick() {
if (tv == 0) tv = 1;
else tv = 0;
}
function heaterClick() {
if (heater == 0) heater = 1;
else heater = 0;
}
function wardrobeClick() {
if (wardrobe == 0) wardrobe = 1;
else wardrobe = 0;
}
function heatingClick() {
if (heating == 0) heating = 1;
else heating = 0;
}
function internetClick() {
if (internet == 0) internet = 1;
else internet = 0;
}
function sofaClick() {
if (sofa == 0) sofa = 1;
else sofa = 0;
}
$(function () {
$("#next_submit").on("click",function () {
$.ajax({
url:"${pageContext.request.contextPath}/house/responsePost3.do",
type:"post",
dataType:"json",
data:{
bed:bed,
washing:washing,
air:air,
balcony:balcony,
ice:ice,
toilet:toilet,
kitchen:kitchen,
tv:tv,
heater:heater,
wardrobe:wardrobe,
heating:heating,
internet:internet,
sofa:sofa
},
success:function () {
window.location.href="${pageContext.request.contextPath}/house/doHousePost.do"
}
})
})
})
</script>
页面效果
点击如下图片中的“发布房源”
填写“房源基本信息”后点击“下一步”,出现的新页面如下
填写“房源详细信息”后点击“下一步”,出现的新页面如下
填写“房源内饰信息”后点击“提交”,出现的新页面如下
可在数据库“tb_house_inter”表中查看到相关信息