Skip to content

Commit

Permalink
2019-08-24完成论坛搜索功能
Browse files Browse the repository at this point in the history
  • Loading branch information
qq744287383 committed Aug 24, 2019
1 parent 997190d commit c524562
Show file tree
Hide file tree
Showing 36 changed files with 1,079 additions and 516 deletions.
Original file line number Diff line number Diff line change
@@ -1,50 +1,57 @@
package com.community.jian.community.Controller;

import com.community.jian.community.exception.ApplicationErrorMessage;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/error")
public class CutomizeController implements ErrorController {


@Override
public String getErrorPath() {
return "error";
}
@RequestMapping(
produces = {"text/html"}
)
public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
HttpStatus status = this.getStatus(request);

ModelAndView modelAndView = new ModelAndView("error");
if (status.is4xxClientError()){
modelAndView.addObject("message", ApplicationErrorMessage.REQUEST_ERROR.getMessage());
}
if (status.is5xxServerError()){
modelAndView.addObject("message",ApplicationErrorMessage.SERVICE_HOT.getMessage());
}

return modelAndView;
}
protected HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer)request.getAttribute("javax.servlet.error.status_code");
if (statusCode == null) {
return HttpStatus.INTERNAL_SERVER_ERROR;
} else {
try {
return HttpStatus.valueOf(statusCode);
} catch (Exception var4) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
}
}
}
package com.community.jian.community.Controller;

import com.community.jian.community.dto.Pioneer;
import com.community.jian.community.exception.ApplicationErrorMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/error")
public class CutomizeController implements ErrorController {

@Value("${github.client.client_id}")
private String client_id;

@Override
public String getErrorPath() {
return "error";
}
@RequestMapping(
produces = {"text/html"}
)
public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
HttpStatus status = this.getStatus(request);

ModelAndView modelAndView = new ModelAndView("error");
Pioneer pioneer = new Pioneer();
pioneer.setClient_id(client_id);
modelAndView.addObject("pioneer",pioneer);
if (status.is4xxClientError()){
modelAndView.addObject("message", ApplicationErrorMessage.REQUEST_ERROR.getMessage());
}
if (status.is5xxServerError()){
modelAndView.addObject("message",ApplicationErrorMessage.SERVICE_HOT.getMessage());
}

return modelAndView;
}
protected HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer)request.getAttribute("javax.servlet.error.status_code");
if (statusCode == null) {
return HttpStatus.INTERNAL_SERVER_ERROR;
} else {
try {
return HttpStatus.valueOf(statusCode);
} catch (Exception var4) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.community.jian.community.service.QuestionService;
import com.community.jian.community.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -26,9 +27,12 @@ public class IndexController {
public String index(Model model,
HttpServletRequest request,
@RequestParam(name = "page",defaultValue = "1") Integer page,
@RequestParam(name = "size",defaultValue = "7") Integer size) throws Exception {
PaginationDTO paginationDTO = questionService.list(page, size);
@RequestParam(name = "size",defaultValue = "7") Integer size,
@RequestParam(name="search",required = false)String search) throws Exception {

PaginationDTO paginationDTO = questionService.list(search,page, size);
model.addAttribute("paginationDTO",paginationDTO);
model.addAttribute("search",search);
return "index";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ ModelAndView serviceHandeException(HttpServletRequest request, ServiceException
@ExceptionHandler(Exception.class)
ModelAndView handleControllerException(HttpServletRequest request, Throwable e, Model model) {
e.printStackTrace();
Pioneer pioneer = new Pioneer();
pioneer.setClient_id(client_id);
model.addAttribute("pioneer",pioneer);
model.addAttribute("message", ApplicationErrorMessage.SERVICE_HOT.getMessage());
return new ModelAndView("error");
}
Expand Down
112 changes: 59 additions & 53 deletions src/main/java/com/community/jian/community/dto/PaginationDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,85 +6,90 @@
import java.util.List;

@Data
public class PaginationDTO <T> {
public class PaginationDTO<T> {

private List<T> data;//页面数据
private Integer page;//当前页面的码数
private Integer endPage;//末尾页的码数!
private List<Integer> pages;//显示出来的页码按钮的码数
private Integer size;//一页里多少个数据

private boolean showPrevious;//是否显示上一页按钮
private boolean showNext;//是否显示下一页按钮
private boolean showFirstPage;//是否显示首页按钮
private boolean showEndPage;//是否显示末位页的按钮

/**
* @param totalQuestion 总的问题数
* @param page 访问第几页的页码数
* @param size 一页中多少行数据
* @param page 访问第几页的页码数
* @param size 一页中多少行数据
* @return 返回size*(pageNum-1)即offset,sql语句中的limit offset,sise:
* **/
public int initPage(Integer totalQuestion,Integer page,Integer size){

if (totalQuestion<=0){
totalQuestion=0;
**/
public int initPage(Integer totalQuestion, Integer page, Integer size) {
//防止恶意传入数据总数量
if (totalQuestion <= 0) {
totalQuestion = 0;
}

if (size<=0){
this.size=1;
}else {
this.size=size;
//防止恶意传入size
if (size <= 0) {
this.size = 1;
} else {
this.size = size;//完成size初始化
}

if (page<1){
this.page=1;
}
else {
this.page=page;
countEndPage(totalQuestion, size);//完成endPage初始化

//页面最小是1
if (page < 1) {
this.page = 1;
} else {
this.page = page;
}

countEndPage(totalQuestion, size);
//防止page大于endPage
if (this.page > this.endPage) {
this.page = this.endPage;
}//完成page当前页初始化

countPagesList(totalQuestion);//pages完成pages页码按钮列初始化

if (this.page>this.endPage){
this.page=this.endPage;
}
countPagesList(totalQuestion);
initShowPrevious();
initShowNext();
initShouFirstPage();
initShowEndPage();


return this.size*(this.page-1);
return this.size * (this.page - 1);//返回offset偏移量!
}

/**
* @param totalQuestion 总共的数据量
* @param size 页面数据量
* 计算出末尾页码,最后的数据不够一页时也要多加一页
*
* **/
* @param size 页面数据量
* 计算出末尾页码,最后的数据不够一页时也要多加一页
**/
private void countEndPage(Integer totalQuestion, Integer size) {
endPage=0;
if(totalQuestion%size==0){
endPage=totalQuestion/size;
}else {
endPage=totalQuestion/size+1;
this.endPage = 0;
if (totalQuestion % size == 0) {
this.endPage = totalQuestion / size;
} else {
this.endPage = totalQuestion / size + 1;
}
}

/**
* 当前页码偏移3次后回到大于等于末尾页页码就不显示末尾按钮
* **/
**/
private void initShowEndPage() {
if((this.page+3)>=endPage){
this.showEndPage=false;
}else {
this.showEndPage=true;
if ((this.page + 3) >= endPage) {
this.showEndPage = false;
} else {
this.showEndPage = true;
}
}

/**
* 当前页码偏移3次后回到小于等于第一页页码就不显示首页按钮
* **/
**/
private void initShouFirstPage() {
if ((this.page - 3) <= 1) {
this.showFirstPage = false;
Expand All @@ -95,34 +100,35 @@ private void initShouFirstPage() {

/**
* 当前页码等于最后一页时就不显示下一页按钮
* **/
**/
private void initShowNext() {
if (this.page==endPage){
this.showNext=false;
}else {
this.showNext=true;
if (this.page == endPage) {
this.showNext = false;
} else {
this.showNext = true;
}
}

/**
* 当前页面码数为1或者等于0的时候不显示上一页按钮
* **/
**/
private void initShowPrevious() {
if(this.page==1 || this.page==0){
this.showPrevious=false;
}else {
this.showPrevious=true;
if (this.page == 1 || this.page == 0) {
this.showPrevious = false;
} else {
this.showPrevious = true;
}
}

/**
* 获取当前页面左边三个页码 ,当前页码,右边变三个页码
*
* **/
* @param allNum 总的数据条数!
**/
private void countPagesList(Integer allNum) {
pages=new ArrayList<>();
if (allNum==0){
return ;
pages = new ArrayList<>();
if (allNum == 0) {
return;
}
pages.add(this.page);
for (int i = 1; i <= 3; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface QuestionEXTMapper {
void addViewCount(Integer id);
Integer addCommentCount(Integer id);
List<Question> getRelateQuestion(@Param("id") Long id,@Param("tags") String tags);
Integer countSearchQuestion(@Param("search")String search);
List<Question> getSearchQuestion(@Param("search")String search,@Param("offset") Integer page,@Param("size") Integer size);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
public interface QuestionService {
int insertQuestion(Question question);

PaginationDTO list(Integer page, Integer size);

PaginationDTO listById(Integer page, Integer size,Integer id);
QuestionDTO getQuestionDTOById(Integer id);

Expand All @@ -22,4 +20,6 @@ public interface QuestionService {
void addViewCount(Integer id);

List<QuestionDTO> getRelateQuestion(Long id,String tags);

PaginationDTO list(String search, Integer page, Integer size);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -39,13 +40,17 @@ public int insertQuestion(Question question) {

@Override
//获取首页问题列表
public PaginationDTO list(Integer page, Integer size) {
public PaginationDTO list(String search,Integer page, Integer size) {
if (StringUtils.isNoneBlank(search)){
search=search.trim();
search=toConversion(search);
String[] s = StringUtils.split(search," ");
search = Arrays.stream(s).collect(Collectors.joining("|"));
}
PaginationDTO<QuestionDTO> paginationDTO=new PaginationDTO<>();
Integer count= (int)questionMapper.countByExample(new QuestionExample());
Integer count= questionEXTMapper.countSearchQuestion(search);
int offset = paginationDTO.initPage(count, page, size);
QuestionExample questionExample = new QuestionExample();
questionExample.setOrderByClause("id desc");
List<Question> questions=questionMapper.selectByExampleWithRowbounds(questionExample,new RowBounds(offset,paginationDTO.getSize()));
List<Question> questions=questionEXTMapper.getSearchQuestion(search,offset,paginationDTO.getSize());
List<QuestionDTO> questionDTOs = getQuestionDTOS(questions);
paginationDTO.setData(questionDTOs);
return paginationDTO;
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

#\u5355\u4E2A\u6587\u4EF6\u7684\u6700\u5927\u9650\u5236
spring.servlet.multipart.max-file-size=100MB
#\u5355\u6B21\u4E0A\u4F20\u6587\u4EF6\u7684\u6700\u5927\u9650\u5236
spring.servlet.multipart.max-request-size=1000MB

##github\u5F00\u53D1\u8005\u914D\u7F6E
github.client.client_id=0d224940fab253243aa6
Expand Down
Binary file modified src/main/resources/db/community.mv.db
Binary file not shown.
Loading

0 comments on commit c524562

Please sign in to comment.