内容分页
1、加载 jar 包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
2、在applicationContext.xml中添加
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect">mysql</prop>
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
3、修改服务层代码
在findAll函数中添加两个参数 page 当前页码,size 每个页面显示的条目数
@Override
public List<UserInfo> findALl(int page, int size) {
PageHelper.startPage(page, size);
return userInfoDao.findAll();
}
4、修改控制层代码
@RequestMapping("/allUser.do")
//这里设置了默认参数,不设置的话会报错
public ModelAndView findAll(@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "5") int size) {
ModelAndView mv = new ModelAndView();
//下面这一行调用了分页类
List<UserInfo> userInfos = userInfoService.findALl(page,size);
PageInfo pageInfo = new PageInfo(userInfos);
mv.addObject("pageInfo", pageInfo);
mv.setViewName("user-list");
return mv;
}
5、修改user-list.jsp
将items修改为${pageInfo.list},同时在跳转链接后面加上page=1&size=5
添加具体分页逻辑
<li><a href="${pageContext.request.contextPath}/user/allUser.do?page=1&size=5" aria-label="Previous">首页</a></li>
<li> <a href="${pageContext.request.contextPath}/user/allUser.do?page=${pageInfo.pageNum-1}&size=5">上一页</a>
</li>
<c:forEach begin="1" end="${pageInfo.pages}" var= "pageNumber">
<li> <a href="${pageContext.request.contextPath}/user/allUser.do?page=${pageNumber}&size = 5">
${pageNumber}</a>
</li>
</c:forEach>
<li><a href="${pageContext.request.contextPath}/user/allUser.do?page=${pageInfo.pageNum+11}&size=5">下一页</a></li>
<li><a href="${pageContext.request.contextPath}/user/allUser.do?page=${pageInfo.pages}&size=5" aria-label="Next">尾页</a></li>
成功的分成了两页
上传图片
1、加载jar包
在 pom.xml中添加
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
2、applicationContext.xml添加bean
<!--文件上传-->
<bean id ="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>
3、控制层
//如果只是上传一个文件,则只需要MultipartFile类型接收文件即可,而且无需显式指定@RequestParam注解
//如果想上传多个文件,那么这里就要用MultipartFile[]类型来接收文件,并且还要指定@RequestParam注解
//并且上传多个文件时,前台表单中的所有<input type="file"/>的name都应该是myfiles,否则参数里的myfiles无法获取到所有上传的文件
@RequestMapping("/uploadPic.do")
public ModelAndView fildUpload( @RequestParam(value = "file",
required = false) MultipartFile file,
HttpServletRequest request) throws Exception {
//获得物理路径webapp所在路径
String pathRoot =
request.getSession().getServletContext().getRealPath("");
//将数据流上传至服务器
file.transferTo(new File(pathRoot,file.getOriginalFilename()));
ModelAndView mv = new ModelAndView();
mv.setViewName("../index");
return mv;
}
<form action="${pageContext.request.contextPath}/user/uploadPic.do" method="post"
enctype="multipart/form-data">
<label>图片</label><input type="file" name="file"/><br/>
<input type="submit" value="提 交"/>
</form>