尝试利用JSF作为Spring MVC的视图(探索篇之一)之表格的排序与选择

本文介绍如何在JSF与SpringMVC整合的基础上,通过添加排序和选择功能增强表格组件的交互性,包括实现事件监听和更新消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注意: 为了方便大家参考最终的代码,特此创建优快云技术博客专用的Git仓库https://github.com/redbeyond/优快云TecBlogRepo.git,需要的朋友可以利用一下。

  在尝试利用JSF作为Spring MVC的视图(初级篇)中,我们已经把JSF配置成为了Spring MVC的视图并能够正常显示数据了。但是我们只是相当于简单地把JSP替换成了JSF而已,接下来我们继续探索高级一些的内容,这里我们要探索的是页面的事件。
  我们要对初级篇中的应用做一些改进,将显示的表格加上排序和选择的功能,请参照下面的步骤和代码。
  首先添加一个类StudentView来对应页面,作为JSF页面的后台Bean

package org.study.basic.mvc.view;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;
import org.study.basic.repository.data.Student;

import lombok.Data;

@Data
public class StudentView {
	private Student selectedStudent;
	private List<Student> students;

	// 对应的是数据行选择事件
	public void onRowSelect(SelectEvent event) {
		FacesMessage msg = new FacesMessage("Student Selected", ((Student) event.getObject()).getName());
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	// 对应的是数据行选择取消事件
	public void onRowUnselect(UnselectEvent event) {
		FacesMessage msg = new FacesMessage("Student Unselected", ((Student) event.getObject()).getName());
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}
}

  接着修改Controller的代码如下,关键点是利用注解@SessionAttributesStudentView的对象加入到了Session中,这样才能保持,对应了JSFManaged Bean

package org.study.basic.mvc.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.study.basic.mvc.view.StudentView;
import org.study.basic.repository.data.Student;
import org.study.basic.service.StudentService;

@Controller
@RequestMapping("/student")
@SessionAttributes("studentView")
public class StudentController {

	@Autowired
	private StudentService studentService;

	@RequestMapping(value = "/info", method = RequestMethod.GET)
	public String showInfo(Model model) {
		StudentView studentView = new StudentView();
		List<Student> students = this.studentService.findAllStudents();
		studentView.setStudents(students);
		model.addAttribute("studentView", studentView);
		return "showInfo";
	}
}

  然后将页面文件从/WEB-INF/中移到页面的根目录下面,具体就是webapp/views/,并将页面showInfo.xhtml进行如下的修改,主要是加上了列排序Ajax事件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:p="http://primefaces.org/ui">

<h:head></h:head>
<h:body>
	<h:form id="form">
		<p:growl id="msgs" showDetail="true" />
		<p:dataTable value="#{studentView.students}" var="student"
			selectionMode="single" rowKey="#{student.id}">
			<f:facet name="header">全体学生</f:facet>
			<p:ajax event="rowSelect" listener="#{studentView.onRowSelect}"
				update=":form:msgs" />
			<p:ajax event="rowUnselect" listener="#{studentView.onRowUnselect}"
				update=":form:msgs" />
			<p:column headerText="姓名" sortBy="#{student.name}">
				<h:outputText value="#{student.name}" />
			</p:column>
			<p:column headerText="年龄" sortBy="#{student.age}">
				<h:outputText value="#{student.age}" />
			</p:column>
			<p:column headerText="特长">
				<h:outputText value="#{student.description}" />
			</p:column>
		</p:dataTable>
	</h:form>
</h:body>
</html>

  同时别忘了修改mvc-servlet.xmlviewResolverprefix

	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/views/" />
		<property name="suffix" value=".xhtml" />
	</bean>

  最后我们看看运行结果:
排序结果
选择学生
取消选择学生
  注意:取消选择需要按住CTRL再用鼠标左键点击。
  好了,本次的探索就到这里。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值