Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 26th, 2012  |  syntax: None  |  size: 5.22 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Spring MVC - Liferay - Validation (with @valid annotation)
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.     xmlns:p="http://www.springframework.org/schema/p"
  6.     xmlns:mvc="http://www.springframework.org/schema/mvc"
  7.     xmlns:context="http://www.springframework.org/schema/context"
  8.     xsi:schemaLocation="
  9.         http://www.springframework.org/schema/mvc
  10.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd    
  11.         http://www.springframework.org/schema/beans
  12.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  13.         http://www.springframework.org/schema/context
  14.         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  15.     <context:component-scan base-package="com.test.sample.sample.ipc.test" />
  16.     <mvc:annotation-driven  />
  17.     <bean id="viewResolver"
  18.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  19.         <property name="viewClass"
  20.             value="org.springframework.web.servlet.view.JstlView" />
  21.         <property name="prefix" value="/WEB-INF/view/test/" />
  22.         <property name="suffix" value=".jsp" />
  23.     </bean>
  24. </beans>
  25.        
  26. <portlet-app version="2.0"
  27.     xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
  28.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.     xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd
  30.         http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd">
  31.     <portlet>
  32.         <portlet-name>test</portlet-name>
  33.         <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
  34.         <init-param>
  35.         <name>contextConfigLocation</name>
  36.         <value>
  37.             /WEB-INF/context/test-portlet.xml
  38.         </value>
  39.     </init-param>
  40.         <supports>
  41.             <mime-type>text/html</mime-type>
  42.             <portlet-mode>view</portlet-mode>
  43.         </supports>
  44.         <portlet-info>
  45.             <title>Test</title>
  46.         </portlet-info>      
  47.  
  48.     </portlet>    
  49. </portlet-app>
  50.        
  51. package com.test.sample.sample.ipc.test;
  52.  
  53. import javax.validation.constraints.Max;
  54. import javax.validation.constraints.Min;
  55. import javax.validation.constraints.NotNull;
  56.  
  57. import org.hibernate.validator.constraints.NotEmpty;
  58.  
  59. public class JavaBean {
  60.  
  61.     private String testName;
  62.  
  63.     public void setTestName(String testName) {
  64.         this.testName = testName;
  65.     }
  66.  
  67.     @NotEmpty(message = "{err.test.notEmpty}")
  68.     @NotNull(message = "{err.test.notNull}")
  69.     @Max(message = "{err.test.max}", value = 10)
  70.     @Min(value = 1, message = "{err.test.min}")
  71.     public String getTestName() {
  72.         return testName;
  73.     }  
  74. }
  75.        
  76. package com.test.sample.sample.ipc.test;
  77. import javax.portlet.ActionRequest;
  78. import javax.portlet.ActionResponse;
  79. import javax.portlet.RenderRequest;
  80. import javax.portlet.RenderResponse;
  81. import javax.validation.Valid;
  82.  
  83. import org.springframework.stereotype.Controller;
  84. import org.springframework.validation.BindingResult;
  85. import org.springframework.web.bind.annotation.RequestMapping;
  86. import org.springframework.web.bind.support.SessionStatus;
  87. import org.springframework.web.portlet.bind.annotation.ActionMapping;
  88.  
  89. import com.test.auction.services.model.Test;
  90. import com.test.auction.services.service.TestLocalServiceUtil;
  91.  
  92. @Controller
  93. @RequestMapping("VIEW")
  94. public class TestController {
  95.  
  96.     @RequestMapping
  97.     public String doView(RenderRequest request, RenderResponse response) {          
  98.         return "test";
  99.     }
  100.  
  101.     @ActionMapping("addTest")
  102.     public void addTestAction(@Valid JavaBean bean, BindingResult result, SessionStatus status, ActionRequest request, ActionResponse response)
  103.     {
  104.         try {
  105.             Test test = TestLocalServiceUtil.addTest(bean.getTestName());
  106.             test.setName("test");
  107.             request.setAttribute("testAttr", "OK");
  108.         } catch (Exception e) {
  109.             e.printStackTrace();
  110.             request.setAttribute("testAttr", "ERROR");
  111.         }
  112.         status.setComplete();      
  113.     }
  114.  
  115. }
  116.        
  117. <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
  118. <%@ taglib prefix="portlet" uri="http://java.sun.com/portlet" %>
  119. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  120. <%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
  121.  
  122. <%@ page import="javax.portlet.PortletPreferences" %>
  123.  
  124. <portlet:defineObjects/>
  125.  
  126. <portlet:actionURL name="addTest" var="editURL"/>
  127.  
  128. <c:out value="${testAttr}" />
  129.  
  130. <aui:form action="<%= editURL %>" name="addTest" method="post">
  131.     <aui:input label="Enter name of new Test Entity :" name="testName" type="text" value="" />
  132.     <aui:button type="submit" />
  133. </aui:form>
  134.        
  135. <mvc:annotation-driven validator="validator" />
  136.  
  137. <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
  138.  
  139. <bean id="annotationMethodHandlerAdapter" class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  140.     <property name="webBindingInitializer">
  141.         <bean id="configurableWebBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
  142.             <property name="validator">
  143.                 <ref bean="validator"/>
  144.             </property>
  145.         </bean>
  146.     </property>
  147. </bean>