How to use VelocityLayoutViewResolver in spring-webmvc with velocity - spring-mvc

<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
<property name="prefix" value="" />
<property name="suffix" value=".vm"></property>
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="layoutUrl" value="layout/default.vm" />
</bean>
how the key word "layoutUrl" work in VelocityLayoutViewResolver?

Its very common to have a dynamic web page divided into a layout part and a content part. The layout part might consist of a header, a footer, a sidebar, a navigation and so on. Elements meant to look more or less the same on every response, that is. But the content part differs, because that's where the action goes on, right?
Layout and content should be kept apart in different .vm files, so that the layout has to be designed (and changed) only once and the content part doesn't have to repeat anything.
The question is how to put those two parts together on each response. One approach is to parse the layout file in every content file. But as the layout usually wraps the content this very likely leads to more than one parsed layout file per content file.
A better way is to reverse that and to merge the content into the layout. This is way easier to handle. All you have to do is to declare a .vm file to work as the general layout file. In this file you put a var named $screen_content and magically the view you returned in your controller at a certain request is blended in at that spot.
Your layoutUrl property tells path and file name of your layout file relative to the resourceLoaderPath you have declared in this bean
<bean
id="velocityConfig"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<beans:property name="resourceLoaderPath" value="/WEB-INF/templates/" />
</bean>
Following your example...
<bean
id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
...
<property name="layoutUrl" value="layout/default.vm" />
</bean>
...your layout file has to be /WEB-INF/templates/layout/default.vm

Related

alfresco evaluator on custom component

I've made a component and I'd like to show that component on the document-details page only if the document has a certain aspect, so I tried to use an evaluator. I've added this bean in custom-slingshot-application-context.xml
<bean id="evaluator.doclib.metadata.hasInvoiceAspect"
parent="evaluator.doclib.action.propertyNotNull">
<property name="property" value="inv:invoice"/>
</bean>
In document-details.xml I added this
<component>
<region-id>custom-comp</region-id>
<sub-components>
<sub-component id="default">
<evaluations>
<evaluation>
<evaluators>
<evaluator type="evaluator.doclib.metadata.hasInvoiceAspect"/>
</evaluators>
<url>/components/custom/custom-comp</url>
</evaluation>
</evaluations>
</sub-component>
</sub-components>
</component>
I guess I've missed something as the component doesn't show up. I have 2 files named custom-slingshot, I tried both tomcat/shared/classes/alfresco/web-extension and tomcat/webapps/share/WEB-INF/classes/alfresco/web-extension.
Am I completely wrong with this? Is it possible to achieve what I want like that? How should I proceed?
There is a predefined evaluator to check whether a node has an aspect called evaluator.doclib.action.hasAspect. You simply need to create a bean with this evaluator as the parent and give an aspect to check against in the properties:
*added to some -context.xml in web-extension
<bean id="my.custom.evaluator" parent="evaluator.doclib.action.hasAspect">
<property name="aspects">
<list>
<value>my:hasInvoiceAspect</value>
</list>
</property>
</bean>
Then you'll reference that bean in your DocLibActions config added to *extension-modules.xml (web-extension/site-data/extensions)...
<config condition="DocLibActions" evaluator="string-compare">
<actions>
<action...>
<!-- Custom evaluator -->
<evaluator>my.custom.evaluator</evaluator>
</action>
</actions>
</config>
Voila.
More info on predefined evaluators.
Good sample project here.

spring mvc with entire html template

I have a spring-boot app using that uses spring-mvc with html. No Thymeleaf, no JSP. I would like to be able to apply themes to my app, much the way CMS's such as Joomla and Wordpress do. The problem is that every Spring-MVC Template article/posting talks about either using a single css file, or using something like Tiles. If I have 15 themes, each in their own folder (they typically seem to have many css, js, and html files), I am not sure how I can apply that theme to my app dynamically (selecting via drop down for example).
Has anyone done anything like this? Conceptually I don't see the problem, but short of manually moving each template related file under /template, I don't know how to best accomplish this.
Using Velocity as viewResolver is possible to do it.
I'm using this configuration:
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/views/"/>
<property name="velocityProperties">
<props>
<prop key="input.encoding">UTF-8</prop>
<prop key="output.encoding">UTF-8</prop>
</props>
</property>
</bean>
<!-- #Velocity -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
<property name="cache" value="true" />
<property name="prefix" value="" />
<property name="suffix" value=".vm" />
<property name="layoutUrl" value="layout1.vm" />
<property name="contentType" value="text/html;charset=UTF-8" />
</bean>
The property layoutUrl there is your DEFAULT template, that is a HTML file inside your webapp/WEB-INF/views/ folder:
layout1.vm:
<html>
<body>
<h1>Hello world 1!</h1>
$screen_content
</body>
</html>
The velocity View Resolver will replace $screen_content with the view contents of your controller response:
MyController.java
...
#RequestMapping("/mycontroller")
public String myController() {
return "myView1";
}
...
So, if the view myView1.vm inside webapp/WEB-INF/views/ is something like:
<h2> Foo Bar! </h2>
The result of a request to /myApp/mycontroller would be like:
<html>
<body>
<h1>Hello world 1!</h1>
<h2> Foo Bar! </h2>
</body>
</html>
And if you want to use another TEMPLATE, you can set it dynamically on your controller, setting the value on your Model var:
...
#RequestMapping("/mycontrollerWithADifferentLayout")
public String myController2(Model m) {
m.addAttribute("layout", "layout2");
return "myView1";
}
...
When setting "layout" attribute on model, Velocity will use the provided view as the template.
Found this on Spring Reference:
17.9.2 Defining themes
To use themes in your web application, you must set up an
implementation of the org.springframework.ui.context.ThemeSource
interface. The WebApplicationContext interface extends ThemeSource but
delegates its responsibilities to a dedicated implementation. By
default the delegate will be an
org.springframework.ui.context.support.ResourceBundleThemeSource
implementation that loads properties files from the root of the
classpath. To use a custom ThemeSource implementation or to configure
the base name prefix of the ResourceBundleThemeSource, you can
register a bean in the application context with the reserved name
themeSource. The web application context automatically detects a bean
with that name and uses it.
When using the ResourceBundleThemeSource, a theme is defined in a
simple properties file. The properties file lists the resources that
make up the theme. Here is an example:
styleSheet=/themes/cool/style.css
background=/themes/cool/img/coolBg.jpg The keys of the properties are
the names that refer to the themed elements from view code. For a JSP,
you typically do this using the spring:theme custom tag, which is very
similar to the spring:message tag. The following JSP fragment uses the
theme defined in the previous example to customize the look and feel:
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<link rel="stylesheet" href="<spring:theme code='styleSheet'/>" type="text/css"/>
</head>
<body style="background=<spring:theme code='background'/>">
...
</body> </html>
By default, the ResourceBundleThemeSource uses an empty base name prefix. As a > result, the properties files are loaded
from the root of the classpath. Thus you would put the cool.properties
theme definition in a directory at the root of the classpath, for
example, in /WEB-INF/classes. The ResourceBundleThemeSource uses the
standard Java resource bundle loading mechanism, allowing for full
internationalization of themes. For example, we could have a
/WEB-INF/classes/cool_nl.properties that references a special
background image with Dutch text on it.
http://docs.spring.io/spring/docs/4.1.x/spring-framework-reference/html/mvc.html#mvc-themeresolver-defining

Spring property editor doesn't work

I've come to spring from jsf, and I'm new in this,,, I want to have a converter for my IdField class,,, I did some research and wrote my own property editor,,,
public class IdFieldPropertyEditor extends PropertyEditorSupport {
and I registered it in dispatcher-servlet.xml
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="com.example.IdField">
<bean class="com.example.IdFieldPropertyEditor" />
</entry>
</map>
</property>
</bean>
so , as I understood these steps are enough, but I still get an error like cannon convert from String to IdField,,,
can anyone help to understand what steps I missed? Thanks,,,
CustomEditorConfigurer has nothing to do with Spring MVC, it configures property editors for interpreting values in XML config files.
To configure property editors for specific controller, use #InitBinder-annotated method. To do it globally for all controllers, use custom WebBindingInitializer. See 15.3.2.12 Customizing WebDataBinder initialization.

How to properly configure Spring.NET in a web context

I'm having a bit of a problem keeping values from appearing in scope for all users. I'm not sure what exactly I may be doing wrong, but I'm at wits end. I'm thinking either I have my container objects scoped wrong or I'm passing the values along the WebContext. First, here's what I'm doing in code. This works, but it's showing details for the first user who logs into the site (note: my PageStore is just a property of type IDictionary that stores values to share to all user controls on the page):
using (BasePage page = (BasePage)(Spring.Web.UI.Page)this.Context.Handler) {
if ((page.PageStore("LoginId") != null)) {
this.Model.LoginId = Convert.ToString(page.PageStore("LoginId"));
}
}
Second, I've tried different scopes for my objects but I'm coming up empty. Here is how my objects are currently configured in the container:
Snippet of my business tier configuration
<!-- Transaction Management Strategy - local database transactions -->
<object id="transactionManager"
type="Spring.Data.Core.AdoPlatformTransactionManager, Spring.Data" scope="request">
<property name="DbProvider" ref="MyProvider"/>
</object>
<tx:attribute-driven transaction-manager="transactionManager" />
<!-- Data Access Objects -->
<object id="DataAccess" type="MyApp.Business.DataAccess.DataAccess, MyApp.Business.DataAccess" abstract="true" scope="request">
<property name="AdoTemplate" ref="MyTemplate" />
</object>
<object id="ManagerDAO" type="MyApp.Business.DataAccess.ManagerDAO, MyApp.Business.DataAccess" parent="DataAccess" scope="request">
<property name="TableName" value="vw_managers" />
<property name="IsAllowedDeletion" value="False" />
<property name="IsAllowedUniqueIdRetrieval" value="False" />
</object>
<!-- Services -->
<object id="SearchService" type="MyApp.Business.Services.SearchService, MyApp.Business.Services" scope="request">
<property name="ManagerDAO" ref="UserDAO" />
</object>
Snippet of my presentation layer:
<object id="BasePageModel" type="MyApp.Presentation.Models.BasePageModel, MyApp.Presentation" abstract="true" scope="session"/>
<object id="ManagerModel" type="MyApp.Presentation.Models.ManagerModel, MyApp.Presentation" parent="BasePageModel" scope="session"/>
<!-- Presenters-->
<object name="ManagerPresenter" type="MyApp.Presentation.Presenters.ManagePresenter, MyApp.Presentation" singleton="false" scope="session">
<property name="SearchSvc" ref="SearchService"/>
</object>
Snippet of my UI layer objects:
<!-- Pages -->
<object id="BasePage" type="MyApp.UI.BasePage, MyApp.UI" abstract="true" singleton="false" scope="session" />
<object id="StandardPage" parent="BasePage" abstract="true" singleton="false" scope="session">
<property name="MasterPageFile" value="~/Layouts/Site.master"/>
</object>
<object type="~/Pages/Manager.aspx" parent="StandardPage" singleton="false" scope="session"/>
I know there are 100 ways to skin this cat, but the fastest solution is to just get this configured properly and drive on. Is my configuration flaky or am I going about this all wrong?
Edit: Common sense is starting to kick in....IDictionary isn't thread safe.
Fixed my problem. Switched my Dictionary objects to thread safe ConcurrentDictionary objects and fixed my scoping issues. For the scopes, my business tier objects are singletons, my model objects are in the session scope, and my views(user controls) and pages are now set to the request scope, non-singletons.

Creating a Web Form from an XML Description of It

i am assigned to web part of some project.My duty is to create web form from xml.
These xml comes from window part and tells which controls are include in my web form and their properties.My xml Format is like below.
<Object type="System.Windows.Forms.Form">
<Property name="Name">Form1</Property>
<Property name="Text">Option</Property>
<Object type="System.Windows.Forms.RadioButton">
<Property name="Name">RadioButton1</Property>
<Property name="Size">86, 24</Property>
<Property name="Text">RadioButton1</Property>
<Property name="Location">175, 126</Property>
</Object>
<Object type="System.Windows.Forms.CheckBox">
<Property name="Name">CheckBox1</Property>
<Property name="Size">84, 24</Property>
<Property name="Text">Accept</Property>
<Property name="Location">84, 126</Property>
</Object>
<Object type="System.Windows.Forms.TextBox">
<Property name="Name">TextBox1</Property>
<Property name="Size">177, 20</Property> 1
<Property name="Text">Singapore</Property>
<Property name="Location">84, 88</Property>
</Object>
<Object type="System.Windows.Forms.Label">
<Property name="Name">Label1</Property>
<Property name="Size">100, 23</Property>
<Property name="Text">Name</Property>
<Property name="Location">7, 53</Property>
</Object>
<DataSet1>
<Table1>
<TableName>TableOne</TableName>
<ItemName>item001</ItemName>
<Qty>100</Qty>
<Price>1000</Price>
</Table1>
<Table2>
<TableName>TableTwo</TableName>
<ItemName>item002</ItemName>
<Qty>200</Qty>
<Price>2000</Price>
</Table2>
</DataSet1>
</Object>
what is the best way to solve my problem? i found some people used
XmlSerialization, so, can i use this way or is there any other way?
I hope you don't expect to be adding Windows Forms controls onto your web form.
This doesn't make much sense. You will not only need to create a web form - you also need to be able to create code to handle events from the controls on the form. I don't see anything in your XML that describes that.

Resources