createValueExpression

本文详细介绍了Java中EL(Expression Language)的ExpressionFactory类的功能与用法,包括如何创建ValueExpression和MethodExpression实例,以及如何将对象转换为特定类型。此外还介绍了创建ExpressionFactory实例的方法。

javax.el 
Class ExpressionFactory

java.lang.Object
  extended by javax.el.ExpressionFactory

public abstract class ExpressionFactory
   
    extends 
    Object
   


Constructor Summary
ExpressionFactory() 
           
 
Method Summary
abstract  ObjectcoerceToType(Object obj, Class<?> targetType) 
          Coerces an object to a specific type according to the EL type conversion rules.
abstract  MethodExpressioncreateMethodExpression(ELContext context, String expression, Class<?> expectedReturnType, Class<?>[] expectedParamTypes) 
          Parses an expression into a MethodExpression for later evaluation.
abstract  ValueExpressioncreateValueExpression(ELContext context, String expression, Class<?> expectedType) 
          Parses an expression into a ValueExpression for later evaluation.
abstract  ValueExpressioncreateValueExpression(Object instance, Class<?> expectedType) 
          Creates a ValueExpression that wraps an object instance.
static ExpressionFactorynewInstance() 
          Creates a new instance of a ExpressionFactory.
static ExpressionFactorynewInstance(Properties properties) 
          Create a new instance of a ExpressionFactory, with optional properties.
 
Methods inherited from class java.lang.Object
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait
 

Constructor Detail

ExpressionFactory

public ExpressionFactory()
Method Detail

newInstance

public static ExpressionFactory newInstance()
Creates a new instance of a  ExpressionFactory. This method uses the following ordered lookup procedure to determine the  ExpressionFactoryimplementation class to load:
  • Use the Services API (as detailed in the JAR specification). If a resource with the name of META-INF/services/javax.el.ExpressionFactoryexists, then its first line, if present, is used as the UTF-8 encoded name of the implementation class.
  • Use the properties file "lib/el.properties" in the JRE directory. If this file exists and it is readable by thejava.util.Properties.load(InputStream) method, and it contains an entry whose key is "javax.el.ExpressionFactory", then the value of that entry is used as the name of the implementation class.
  • Use the javax.el.ExpressionFactory system property. If a system property with this name is defined, then its value is used as the name of the implementation class.
  • Use a platform default implementation.


newInstance

public static ExpressionFactory newInstance(Properties properties)

Create a new instance of a ExpressionFactory, with optional properties. This method uses the same lookup procedure as the one used innewInstance().

If the argument properties is not null, and if the implementation contains a constructor with a single parameter of type java.util.Properties, then the constructor is used to create the instance.

Properties are optional and can be ignored by an implementation.

The name of a property should start with "javax.el."

The following are some suggested names for properties.

  • javax.el.cacheSize

Parameters:
properties - Properties passed to the implementation. If null, then no properties.

createValueExpression

public abstract ValueExpression createValueExpression(ELContext context,
                                                      String expression,
                                                      Class<?> expectedType)
Parses an expression into a  ValueExpression for later evaluation. Use this method for expressions that refer to values.

This method should perform syntactic validation of the expression. If in doing so it detects errors, it should raise an ELException.

Parameters:
context - The EL context used to parse the expression. The  FunctionMapper and  VariableMapper stored in the ELContext are used to resolve functions and variables found in the expression. They can be  null, in which case functions or variables are not supported for this expression. The object returned must invoke the same functions and access the same variable mappings regardless of whether the mappings in the provided  FunctionMapper and  VariableMapper instances change between calling  ExpressionFactory.createValueExpression() and any method on  ValueExpression.

Note that within the EL, the ${} and #{} syntaxes are treated identically. This includes the use of VariableMapper and FunctionMapper at expression creation time. Each is invoked if not null, independent of whether the #{} or ${} syntax is used for the expression.

expression - The expression to parse
expectedType - The type the result of the expression will be coerced to after evaluation.
Returns:
The parsed expression
Throws:
NullPointerException - Thrown if expectedType is null.
ELException - Thrown if there are syntactical errors in the provided expression.

package com.lkm.pmi.nonstdpart.bean; import com.lkm.pmi.mainvc.FacesUtils; import com.lkm.pmi.nonstdpart.enums.DataType; import com.lkm.pmi.nonstdpart.service.NonStdPartTimeService; import com.lkm.pmi.nonstdpart.utils.LabelItem; import com.lkm.pmi.nonstdpart.utils.Parameter; import com.lkm.pmi.template.ServerInfoBean; import org.apache.commons.lang.StringUtils; import org.primefaces.PrimeFaces; import org.primefaces.component.inputtext.InputText; import org.primefaces.component.outputlabel.OutputLabel; import org.primefaces.component.outputpanel.OutputPanel; import org.primefaces.component.spacer.Spacer; import javax.annotation.PostConstruct; import javax.ejb.EJB; import javax.ejb.EJBException; import javax.el.MethodExpression; import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedProperty; import javax.faces.bean.ViewScoped; import javax.faces.component.UIComponent; import javax.faces.component.UIOutput; import javax.faces.component.behavior.AjaxBehavior; import javax.faces.event.MethodExpressionValueChangeListener; import javax.faces.event.ValueChangeEvent; import java.util.*; import java.util.logging.Logger; @ManagedBean(name = "nonStdPartTimeBean") @ViewScoped public class NonStdPartTimeBean { Logger logger = Logger.getLogger(this.getClass().getName()); private List<LabelItem> labelItemList; private String selectedItem; private String currentEmpNo; private String currentMachineNo; private String orderNo; Map<String, Object> paraMap = new HashMap<>(); @ManagedProperty(value = "#{serverInfoBean}") ServerInfoBean serverInfoBean; @EJB NonStdPartTimeService nonStdPartTimeService; @PostConstruct public void init() { initLabelItemList(); //初始化左边菜单 } //初始化左边菜单 private void initLabelItemList() { //根据登录人获取对应绑定的机床号来显示对应工序类型 currentMachineNo = "E-C E-M E-X E-R"; List<String> labelItemsList = new ArrayList<>(); if (currentMachineNo.contains("E-C")) { labelItemsList.addAll(Arrays.asList( "成品", "成形", "车端面", "车长度", "车内孔", "车内油槽", "托位钻孔", "钻头部牙孔", "托位加牙", "头部加牙", "托位牙倒角", "头部牙倒角", "切断", "钻托位中心孔", "钻胚头孔", "钻注油孔", "车螺纹" )); } if (currentMachineNo.contains("E-M")) { labelItemsList.addAll(Arrays.asList( "粗磨", "精磨", "大托粗磨", "大托精磨", "小托粗磨", "小托精磨", "后托粗精磨", "靠磨端面", "磨长度", "磨厚度", "粗磨厚度", "精磨厚度", "磨疏气位", "磨斜度", "磨内孔" )); } if (currentMachineNo.contains("E-R")) { labelItemsList.addAll(Arrays.asList( "托位淬火", "高频淬火" )); } if (currentMachineNo.contains("E-X")) { labelItemsList.addAll(Arrays.asList( "光头尾", "铣扁位", "铣扳手位" )); } for (String label : labelItemsList) { labelItemList.add(LabelItem.Builder.getInstance(label, label) .addPara("orderNo", "订单号", DataType.String).build()); } logger.info("工序:" + labelItemsList.size()); } public void doSelect(ValueChangeEvent event) { selectedItem = String.valueOf(event.getNewValue()); for (LabelItem item : labelItemList) { if (item.getLabel().equals(event.getNewValue())) { OutputPanel panel = (OutputPanel) FacesUtils.findComponent("para-panel"); panel.getChildren().clear(); List<Parameter> paraList = item.getParaList(); for (Parameter para : paraList) { String veValue = String.format("#{nonStdPartTimeBean.paraMap['%s']}", para.getId()); UIOutput uiLabel = new OutputLabel(); uiLabel.setId(para.getId()); uiLabel.setValue(para.getName() + ":"); panel.getChildren().add(uiLabel); //目前只有订单号 if ("orderNo".equals(para.getId())) { UIComponent ui = new InputText(); ui.setId("order-No"); setComponentPara(ui, panel, veValue); String expressionString = "#{nonStdPartTimeBean.changeOrder}"; MethodExpression expression = FacesUtils.createMethodExpression(expressionString, ValueChangeEvent.class); MethodExpressionValueChangeListener listener = new MethodExpressionValueChangeListener(expression); ((InputText) ui).setValue(""); ((InputText) ui).addValueChangeListener(listener); // 增加监听 AjaxBehavior behavior = new AjaxBehavior(); ((InputText) ui).addClientBehavior("change", behavior); } Spacer spacer = new Spacer(); spacer.setWidth("10"); spacer.setId(String.format("%s-spacer", para.getId())); panel.getChildren().add(spacer); } PrimeFaces.current().ajax().update(panel.getClientId()); } } FacesUtils.getFacesContext().renderResponse(); } private void setComponentPara(UIComponent ui, OutputPanel panel, String veValue) { ui.setValueExpression("value", FacesUtils.createValueExpression(veValue)); ui.getAttributes().put("style", "width:150px;"); panel.getChildren().add(ui); } public void changeOrder(ValueChangeEvent event) { orderNo = (String) event.getNewValue(); orderNo = orderNo.toUpperCase(); // 转大写 } public void submit() { try { currentEmpNo = serverInfoBean.getCurrentEmpno(); if (currentEmpNo.matches("(?i)none|LOGOUT")) { throw new EJBException("请先登录"); } if (StringUtils.isBlank(selectedItem)) { throw new EJBException("请先选择左侧的项目"); } if (StringUtils.isBlank(orderNo)) { throw new EJBException("请填写订单号"); } Map<String,String> paramMap = new HashMap<>(); paramMap.put("orderNo", orderNo); paramMap.put("partName", "支承柱加胚头孔"); // 零件名称 paramMap.put("processName", selectedItem); // 工序 paramMap.put("partDiameter", "80.00"); // 直径 paramMap.put("partLength", "170.00"); // 长度 paramMap.put("machineNo", "E-X-160"); // 机床号 paramMap.put("qty", "4"); // 数量 float totalWorkTime = nonStdPartTimeService.getNonStdPartProTimeByMap(paramMap); if(totalWorkTime > 0){ FacesUtils.showInfo("领取工时", "领取成功:" + totalWorkTime); } } catch (Exception e) { String msg = e.getMessage(); logger.warning(msg); FacesUtils.showWarning("提示", msg); } } public Logger getLogger() { return logger; } public void setLogger(Logger logger) { this.logger = logger; } public List<LabelItem> getLabelItemList() { return labelItemList; } public void setLabelItemList(List<LabelItem> labelItemList) { this.labelItemList = labelItemList; } public String getSelectedItem() { return selectedItem; } public void setSelectedItem(String selectedItem) { this.selectedItem = selectedItem; } public String getCurrentEmpNo() { return currentEmpNo; } public void setCurrentEmpNo(String currentEmpNo) { this.currentEmpNo = currentEmpNo; } public String getCurrentMachineNo() { return currentMachineNo; } public void setCurrentMachineNo(String currentMachineNo) { this.currentMachineNo = currentMachineNo; } public String getOrderNo() { return orderNo; } public void setOrderNo(String orderNo) { this.orderNo = orderNo; } public Map<String, Object> getParaMap() { return paraMap; } public void setParaMap(Map<String, Object> paraMap) { this.paraMap = paraMap; } public ServerInfoBean getServerInfoBean() { return serverInfoBean; } public void setServerInfoBean(ServerInfoBean serverInfoBean) { this.serverInfoBean = serverInfoBean; } public NonStdPartTimeService getNonStdPartTimeService() { return nonStdPartTimeService; } public void setNonStdPartTimeService(NonStdPartTimeService nonStdPartTimeService) { this.nonStdPartTimeService = nonStdPartTimeService; } } 我这里哪里有问题
最新发布
08-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值