SpringMVC+easyui显示数据

近期做毕业设计,想用easyui,先学习一下CRUD。今天先弄了个表格显示数据库的数据。jsp页面还有非常多其他元素,我就不贴上去了。我显示数据的JSP为/WebContent/WEB-INF/views/user/list.jsp 。关键信息例如以下:

	
UserId UserName PassWord Enable
当中 最要注意的是字段,我的entity里面userId实际上是UserId,我把User对象集合转换为JSON格式发送到web前端后。其属性第一个字母都变小写了。

User实体:

package com.yang.bishe.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

@Entity
@Table(name="T_User")
@DynamicInsert(true)
@DynamicUpdate(true)
public class User implements java.io.Serializable {
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="UserId")
	private String UserId; 
	@Column(name="UserName")
	private String UserName;
	@Column(name="PassWord")
	private String PassWord;
	@Column(name="Enable")
	private int Enable;
	public String getUserId() {
		return UserId;
	}
	public void setUserId(String userId) {
		UserId = userId;
	}
	public String getUserName() {
		return UserName;
	}
	public void setUserName(String userName) {
		UserName = userName;
	}
	public String getPassWord() {
		return PassWord;
	}
	public void setPassWord(String passWord) {
		PassWord = passWord;
	}
	public int getEnable() {
		return Enable;
	}
	public void setEnable(int enable) {
		Enable = enable;
	}
}

项目中我用了BaseController,当中封装了转换为json对象的方法,里面要用到一些包注意一下: 

package com.yang.bishe.controller;

import java.io.IOException;
import java.util.Arrays;

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

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.yang.bishe.util.FastjsonFilter;

/**
 * 基础Controller,其它Controller继承此Controller来获得writeJson和ActionSupport的功能
 * 
 * 主要的CRUD已实现。子类继承BaseController的时候,提供setService方法就可以
 * @author yang
 * 
 */
@Controller
public class BaseController {

	/**
	 * 将对象转换成JSON字符串,并响应回前台
	 * 
	 * @param object
	 * @param includesProperties
	 *            须要转换的属性
	 * @param excludesProperties
	 *            不须要转换的属性
	 */
	public void writeJsonByFilter(Object object, HttpServletResponse response,String[] includesProperties, String[] excludesProperties) {
		try {
			FastjsonFilter filter = new FastjsonFilter();// excludes优先于includes
			if (excludesProperties != null && excludesProperties.length > 0) {
				filter.getExcludes().addAll(Arrays. asList(excludesProperties));
			}
			if (includesProperties != null && includesProperties.length > 0) {
				filter.getIncludes().addAll(Arrays. asList(includesProperties));
			}
		//	logger.info("对象转JSON:要排除的属性[" + excludesProperties + "]要包括的属性[" + includesProperties + "]");
			String json;
			String User_Agent = getRequest().getHeader("User-Agent");
			if (StringUtils.indexOfIgnoreCase(User_Agent, "MSIE 6") > -1) {
				// 使用SerializerFeature.BrowserCompatible特性会把全部的中文都会序列化为\\uXXXX这样的格式。字节数会多一些,可是能兼容IE6
				json = JSON.toJSONString(object, filter, SerializerFeature.WriteDateUseDateFormat, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.BrowserCompatible);
			} else {
				// 使用SerializerFeature.WriteDateUseDateFormat特性来序列化日期格式的类型为yyyy-MM-dd hh24:mi:ss
				// 使用SerializerFeature.DisableCircularReferenceDetect特性关闭引用检測和生成
				json = JSON.toJSONString(object, filter, SerializerFeature.WriteDateUseDateFormat, SerializerFeature.DisableCircularReferenceDetect);
			}
		//	logger.info("转换后的JSON字符串:" + json);
			response.setContentType("text/html;charset=utf-8");
			response.getWriter().write(json);
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private HttpServletRequest getRequest() {
		// TODO Auto-generated method stub
		return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	}

	/**
	 * 将对象转换成JSON字符串。并响应回前台
	 * 
	 * @param object
	 * @throws IOException
	 */
	public void writeJson(Object object ,HttpServletResponse response) {
		writeJsonByFilter(object, response,null, null);
	}



}


UserController,执行后,在浏览器地址后面加上user/list 就会跳转到list.jsp ,在list.jsp里面

url的请求地址(注意这个地址是相对路径。我在前面base href已经加上项目地址)就匹配到listUser函数里面: 
    

package com.yang.bishe.controller;

import java.util.List;

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

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.servlet.ModelAndView;

import com.yang.bishe.entity.User;
import com.yang.bishe.service.interfaces.IUserService;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
	@Autowired
	private IUserService userService;
	@RequestMapping("/list")
	   public ModelAndView goList(){
	       // return "/views/index";
		  return new ModelAndView("user/list");
		  
	    }
	@RequestMapping("/listUsers")
	  public String listUser(HttpServletRequest request,
				HttpServletResponse response) throws Exception {
	       // return "/views/index";
		String hql="from User";
		Listusers=userService.find(hql);
	//	String result=userService.find(hql);
		writeJson(users,response);
		
		//System.out.println(users);
		return null;
	    }
}

转载于:https://www.cnblogs.com/lcchuguo/p/5224468.html

你可能感兴趣的:(SpringMVC+easyui显示数据)