/**
   * validation check를 할때 hashMap name이 view(jsp)에서 입력되었는지를 알수 있게 하기 위해서는 stack에 getter setter로 정의된
   * hashMap(여기에서는 reqMap이름을 쓴다)이름을 지정하고 hashMap에 저장된 값을 리턴한다.
   */
  @Override
  protected Object getFieldValue(String name, Object object) throws ValidationException {
    ValueStack stack = ActionContext.getContext().getValueStack();

    boolean pop = false;

    if (!(stack.getRoot().contains(object))) {
      stack.push(object);
      pop = true;
    }

    Object retVal = null;
    // HashMap reqMap의 값을 stack에 넣어준다.
    Object obj = (Map) stack.findValue("reqMap");

    // HashMap reqMap이 null이 아닐때
    if (obj != null) {
      Map m = (Map) obj;
      // stack에 있는 HashMap reqMap값을 name으로 가져와서 valueStack에 올려준다.
      retVal = m.get(name);
    }
    // HashMap reqMap이 null일때
    else {
      // getter setter을 사용한 name의 값을 valueStack에 올려준다.
      retVal = stack.findValue(name);
    }

    if (pop) {
      stack.pop();
    }

    return retVal;
  }
  protected Object getOverrideExpr(ActionInvocation invocation, Object value) {
    ValueStack stack = invocation.getStack();

    try {
      stack.push(value);

      return escape(stack.findString("top"));
    } finally {
      stack.pop();
    }
  }
Пример #3
0
 // 进入修改界面
 public String viewUpdate() {
   // 查到所有部门
   List<Dept> listDept = deptService.getAll();
   request.put("listDept", listDept);
   // 根据点击进入更新页面获得的参数id,查到需要修改的员工的初始信息
   int id = employee.getId();
   Employee emp = employeeService.findById(id);
   ValueStack vsStack = ActionContext.getContext().getValueStack();
   // 移除栈顶
   vsStack.pop();
   // 把获得的员工信息加入栈
   vsStack.push(emp);
   return "edit";
 }
  public void testFieldErrorMessageAddedWhenConversionFailsOnModelDriven() {
    ModelDrivenAnnotationAction action = new ModelDrivenAnnotationAction();
    ValueStack stack = ActionContext.getContext().getValueStack();
    stack.push(action);
    stack.push(action.getModel());

    Map<String, Object> ognlStackContext = stack.getContext();
    ognlStackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);

    String[] value = new String[] {"invalid date"};
    assertEquals(
        "Conversion should have failed.",
        OgnlRuntime.NoConversionPossible,
        converter.convertValue(ognlStackContext, action, null, "birth", value, Date.class));
    stack.pop();
    stack.pop();

    Map conversionErrors = (Map) ognlStackContext.get(ActionContext.CONVERSION_ERRORS);
    assertNotNull(conversionErrors);
    assertEquals(1, conversionErrors.size());
    assertNotNull(conversionErrors.get("birth"));
    assertEquals(value, conversionErrors.get("birth"));
  }
  public void testFieldErrorMessageAddedForComplexProperty() {
    SimpleAnnotationAction action = new SimpleAnnotationAction();
    action.setBean(new AnnotatedTestBean());

    ValueStack stack = ActionContext.getContext().getValueStack();
    stack.push(action);

    Map<String, Object> ognlStackContext = stack.getContext();
    ognlStackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
    ognlStackContext.put(XWorkConverter.CONVERSION_PROPERTY_FULLNAME, "bean.birth");

    String[] value = new String[] {"invalid date"};
    assertEquals(
        "Conversion should have failed.",
        OgnlRuntime.NoConversionPossible,
        converter.convertValue(
            ognlStackContext, action.getBean(), null, "birth", value, Date.class));
    stack.pop();

    Map conversionErrors = (Map) stack.getContext().get(ActionContext.CONVERSION_ERRORS);
    assertNotNull(conversionErrors);
    assertTrue(conversionErrors.size() == 1);
    assertEquals(value, conversionErrors.get("bean.birth"));
  }