我的Java Web之路 - 租房网应用完整代码

本系列文章旨在记录和总结自己在Java Web开发之路上的知识点、经验、问题和思考,希望能帮助更多码农和想成为码农的人。
本文转发自头条号【普通的码农】的文章,大家可以关注一下,直接在今日头条的移动端APP中阅读。因为平台不同,会出现有些格式、图片、链接无效方面的问题,我尽量保持一致。
原文链接:https://www.toutiao.com/i6764266772968243715/

文章目录

    • 介绍
    • 工程结构
    • POM文件
    • Web部署描述符 - web.xml
    • Spring IoC配置元数据 - dispatcher.xml
    • Java代码 - 控制器层
    • Java代码 - 服务层
    • Java代码 - 实体类
    • 静态页面 - login.html
    • JSP页面

介绍

本篇文章给出截止到上篇文章之时,租房网应用的完整代码。

工程结构

我的Java Web之路 - 租房网应用完整代码_第1张图片

POM文件


 4.0.0
 house-renter
 house-renter
 0.0.1-SNAPSHOT
 war
 
 

 org.springframework
 spring-context
 5.2.1.RELEASE



 org.springframework
 spring-webmvc
 5.2.1.RELEASE



 org.apache.taglibs
 taglibs-standard-impl
 1.2.5



 org.apache.taglibs
 taglibs-standard-spec
 1.2.5



 com.h2database
 h2
 1.4.200



 org.springframework
 spring-jdbc
 5.2.1.RELEASE



 com.alibaba
 druid
 1.1.21

 
 
 
 
 maven-compiler-plugin
 3.8.0
 
 1.8
 1.8
 
 
 
 maven-war-plugin
 3.2.1
 
 WebContent
 
 
 
 

Web部署描述符 - web.xml

主要是用来配置 Spring MVC 的 DispatcherServlet 。



	house-renter
	
		index.html
		index.htm
		index.jsp
		default.html
		default.htm
		default.jsp
	
	
		characterEncodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			UTF-8
		
		
			forceEncoding
			true
		
	
	
		characterEncodingFilter
		/*
	
	
		dispatcher
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			/WEB-INF/dispatcher.xml
		
		1
	
	
		dispatcher
		*.action
	

Spring IoC配置元数据 - dispatcher.xml

主要用来开启组件扫描、配置DataSource和JdbcTemplate这两个Bean。



	
	

	
		
		
		

		
		
		
		
	
	
	
		
	

Java代码 - 控制器层

package houserenter.controller;
import java.io.UnsupportedEncodingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import houserenter.entity.House;
import houserenter.service.HouseService;
@Controller
public class HouseRenterController {
	
	@Autowired
	private HouseService houseService;
	@GetMapping("/test.action")
	@ResponseBody
	public String test() {
		return "hello";
	}
	@PostMapping("/login.action")
	public ModelAndView postLogin(String userName, String password) {
		//这里需要验证用户是否已经注册,省略
		System.out.println("userName: " + userName + ", password: " + password);
		ModelAndView mv = new ModelAndView();
		//重定向到查找感兴趣房源列表的动作
		mv.setViewName("redirect:houses.action?userName=" + userName);
		return mv;
	}
	@GetMapping("/houses.action")
	public ModelAndView getHouses(String userName) {
		//这里需要验证用户是否登录,省略
		
		ModelAndView mv = new ModelAndView();
		//查找感兴趣房源并绑定到相应JSP页面,然后将请求转发到该页面
		mv.addObject("mockHouses", houseService.findHousesInterested(userName));
		mv.setViewName("houses.jsp?userName=" + userName);
		return mv;
	}
	@GetMapping("/house-details.action")
	public ModelAndView getHouseDetails(String userName, String houseId) {
		// 这里需要验证用户是否登录,省略
		ModelAndView mv = new ModelAndView();
		//查找房源详情并绑定到相应JSP页面,然后将请求转发到该页面
		mv.addObject("target", houseService.findHouseById(houseId));
		mv.setViewName("house-details.jsp?userName=" + userName);
		return mv;
	}
	@GetMapping("/house-form.action")
	public ModelAndView getHouseForm(String userName, String houseId) {
		// 这里需要验证用户是否登录,省略
		ModelAndView mv = new ModelAndView();
		//查找房源详情并绑定到相应JSP页面,然后将请求转发到该页面
		mv.addObject("target", houseService.findHouseById(houseId));
		mv.setViewName("house-form.jsp?userName=" + userName);
		return mv;
	}
	@PostMapping("/house-form.action")
	public ModelAndView postHouseForm(String userName, House house) throws UnsupportedEncodingException {
		// 这里需要验证用户是否登录,省略
		//更新指定房源的详情
		houseService.updateHouseById(house);
		//将请求转发到查找房源详情的动作
		ModelAndView mv = new ModelAndView();
		mv.setViewName("redirect:house-details.action?userName=" + userName + "&houseId=" + house.getId());
		return mv;
	}
}

Java代码 - 服务层

package houserenter.service;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import houserenter.entity.House;
@Service
public class HouseService {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@PostConstruct
	public void generateMockHouses() {
		jdbcTemplate.execute("drop table if exists house");
		jdbcTemplate.execute("create table house(id varchar(20) primary key, name varchar(100), detail varchar(500))");
		
		jdbcTemplate.update("insert into house values(?, ?, ?)", "1", "金科嘉苑3-2-1201", "详细信息");
		jdbcTemplate.update("insert into house values(?, ?, ?)", "2", "万科橙9-1-501", "详细信息");
	}
	
	public List findHousesInterested(String userName) {
		// 这里查找该用户感兴趣的房源,省略,改为用模拟数据
		return jdbcTemplate.query(
				"select id,name,detail from house", 
				new HouseMapper());
	}
	public House findHouseById(String houseId) {
		return jdbcTemplate.queryForObject(
				"select id,name,detail from house where id = ?",
				new Object[]{houseId},
				new HouseMapper());
	}
	public void updateHouseById(House house) {
		jdbcTemplate.update(
				"update house set id=?, name=?, detail=? where id=?",
				house.getId(), house.getName(), house.getDetail(), house.getId());
	}
	
	private static final class HouseMapper implements RowMapper {

		@Override
		public House mapRow(ResultSet rs, int rowNum) throws SQLException {
			return new House(rs.getString("id"), rs.getString("name"), rs.getString("detail"));
		}
		
	}
}

Java代码 - 实体类

package houserenter.entity;

public class House {

	private String id;
	private String name;
	private String detail;
	public House(String id, String name, String detail) {
		super();
		this.id = id;
		this.name = name;
		this.detail = detail;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDetail() {
		return detail;
	}
	public void setDetail(String detail) {
		this.detail = detail;
	}
	@Override
	public String toString() {
		return "House [id=" + id + ", name=" + name + ", detail=" + detail + "]";
	}
}

静态页面 - login.html





租房网 - 登录•


	

JSP页面

include.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.List" %>
<%@ page import="houserenter.entity.House" %>




租房网


你好,${param.userName}!欢迎来到租房网! 退出



houses.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="include.jsp"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
共找到你感兴趣的房源 ${mockHouses.size()} 条

house-details.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="include.jsp"%>

${target.name}编辑

${target.detail}

回到列表

house-form.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="include.jsp"%>

你可能感兴趣的:(租房网应用完整代码)