二.SpringMVC的集成
2.1 准备applicationContext-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!--1.扫描相应的web层-->
<context:component-scan base-package="cn.itsource.aisell.web" />
<!--2.静态资源放行-->
<mvc:default-servlet-handler />
<!--3.mvc的注解支持-->
<mvc:annotation-driven />
<!--4.视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!--5.上传解析器 CommonsMultipartResolver-->
<!-- 注意:上传解析器的id必需:multipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>10485760</value>
</property>
</bean>
</beans>
2.2 准备web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!--SpringMVC的配置和Spring的配置要单独读取,否则后面集成其它框架会出问题-->
<!--运行与读取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>
<!--配置mvc的核心控制器-->
<servlet>
<servlet-name>dispatchServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--读取mvc配置-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
<!--tomcat启动用创建-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatchServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--编码过滤-->
<filter>
<filter-name>encodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2.3 新建UIPage类
- 由于我们前面要集成Easyui
- Easyui在分页时想要的数据结构
{total:xx,rows:[...]}
- 准备
UIPage
来匹配这个结构
public class UIPage<T> {
private Long total; //总条数
private List<T> rows; //每页数据
//在构造的时候把page对象变成前端需要的结果
public UIPage(Page page) {
total = page.getTotalElements();
rows = page.getContent();
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public List<T> getRows() {
return rows;
}
public void setRows(List<T> rows) {
this.rows = rows;
}
}
2.4 准备Controller
@Controller
@RequestMapping("/employee")
public class EmployeeController extends BaseController {
@Autowired
private IEmployeeService employeeService;
@RequestMapping("/index")
public String index(){
return "employee/index";
}
@RequestMapping("/findAll")
@ResponseBody
public List<Employee> findAll(){
return employeeService.findAll();
}
/**
* 准备分页方法
* @param query
* @return
*/
@RequestMapping("/page")
@ResponseBody
public UIPage page(EmployeeQuery query){
//return employeeService.queryPage(query);
return new UIPage(employeeService.queryPage(query));
}
}