I am using Apache tiles for templating and part of the template is a header text. This text depends on the section the page belongs to. Each page contains a bean and the header text is built using the properties of that bean. The bean will have a different name for each page.
So, in my JSP file I would have something like this:
<div>${myBean.id} - ${myBean.name}</div>
I want to get that expression in the tile definition and I tried this:
<definition template="/WEB-INF/tiles/layout/mytemplate.jsp">
<put-attribute name="title" expression="${myBean.id} - ${myBean.name}" />
</definition>
And in the template I do:
<div class="title-header"><tiles:insertAttribute name="title" /></div>
But the result is the unprocessed EL expression:
<div>${myBean.id} - ${myBean.name}</div>
The code has been simplified here to keep this post concise but this is exactly what I'm trying to do. There are also reasons why I am trying to do it this way.
Any idea why the EL expresion is not being processed?
Thanks
NOTE: I am fairly new to JSP and Apache Tiles so I may not have used the correct terminology.
I just wanted to point out that Barry's answer (in his comment on the original post) helped me out. You need to have tiles-el.jar on your classpath (if you want to use the standard EL; presumably you need the corresponding JARs for MVEL or OGNL).
Tiles 2. Regarding AttributeEvaluator, here's how you can set that up if you're using Spring:
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles/**/views.xml</value>
</list>
</property>
<!-- Initialize expression language support for use in Tiles definitions. -->
<property name="tilesProperties">
<props>
<prop key="org.apache.tiles.evaluator.AttributeEvaluator">org.apache.tiles.evaluator.el.ELAttributeEvaluator</prop>
</props>
</property>
</bean>
Tiles 3. Spring's TilesConfigurer for Tiles 3 automatically checks the classpath for the JSP API 2.1 and Tiles EL JARs. If it finds them both, it automatically creates an EL-aware attribute evaluator.
Related
I'm writing a parsing tool to compare the textual content of two bean XML files in Java. The text content changes and we need a way to run a script to make sure the textual content is the same. I know we have org.w3c.dom which has a method getElementsByTagName("tag_name") and that returns a node list in the XML document. I'm wondering if anyone knows of a way to do this using the class name? I've been searching around but haven't been able to solve this yet.
<bean class="com.mycompany.myText" id="Q1.4">
<property name="name">
<value>Q1.4</value>
</property>
<property name="text">
<value>This is text one</value>
</property>
<property name="retired">
<value>true</value>
</property>
</bean>
<bean class="com.mycompany.myText" id="Q1.5">
<property name="name">
<value>Q1.5</value>
</property>
<property name="text">
<value>This is text two</value>
</property>
<property name="retired">
<value>true</value>
</property>
</bean>
I can't use the 'bean' element name as there are several other beans whith non relevant stuff I need only the ones with the class com.mycompany.myText and the value I'm trying to extract is property name=text - the text content
Any help here would be appreciated. Also as a side note I should mention that we have no direct control of how the XML file is managed and structured as it's fed to us from a 3rd party.
As you say, you need to select tags by their attributes.
You could use XPath to achieve this. See those resources:
http://viralpatel.net/blogs/java-xml-xpath-tutorial-parse-xml/
https://stackoverflow.com/a/24220968/6377268
The expression would be something like this
/bean[#class='com.mycompany.myText']/property[#name='text']/value
I hope I could help at least a little bit.
I would like to have a external property (which has label name & value pairs) for each jsp pages in my web application.
I've done in this way like simply reading property file in a Map and placing in jsp using expression language.
Is there any other methodology to handle these in Java/Spring?
You can have these as message properties
http://www.mkyong.com/spring-mvc/spring-mvc-internationalization-example/
This also explains this in detail - http://www.journaldev.com/2610/spring-mvc-internationalization-i18n-and-localization-l10n-example
If you need these to be by locale, you can use localechangeInterceptor also.
You can PropertyPlaceholderConfigurer for external properties (eg the systemPropertiesMode property)
http://www.summa-tech.com/blog/2009/04/20/6-tips-for-managing-property-files-with-spring
There are also more ways to do this like http://howtodoinjava.com/2015/01/27/how-to-load-external-resources-files-into-spring-context/
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>locale\customer\messages</value>
</property>
</bean>
spring mvc and ReloadableResourceBundleMessageSource
Yes there is another way to show the labels in spring and this one is the best practice code.
enter link description here This will explain you how to use <spring:message code="label.viewUser" /> in your project.
I have tiles set up in my spring project as the view handler like this:
<bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver" />
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/views/**/tiles.xml</value>
</list>
</property>
</bean>
I'd like to use EL to access the session scope inside tiles.xml, for resolving a jsp file name. This should be possible if i use CompleteAutoloadTilesContainerFactory, as described here:
http://tiles.apache.org/framework/tutorial/advanced/el-support.html
How can I set my spring tiles configuration to allow this. Ive tried using EL as it is currently set up but the EL is not parsed.
Figured it out, i left the configuration exactly the same and just included tiles-el.jar . No need to include tiles-extras.jar though. NFV
You may try setting completeAutoload property of TilesConfigurer class. Please see reference doc TilesConfirurer.
This may require Tiles-Extras 2.2 jar file.
My starting problem is this error message:
Problem accessing /segment.htm.
Reason:
Neither BindingResult nor plain target object for bean name
'acceptCorrected' available as request
attribute
The top-level description of what I'm working on is this:
Form1 solicits some input from the user. When form1 submits, I need to push that data through some processing, and then present form2, containing the results of the processing. I am trying to communicate the results of form1 to form2 via the model returned by form1's controller's onSubmit.
There's reason to believe that this is verboten.
But, if it is, how do I get the data from1 to be available when rendering the JSP page for form2?
<bean name="/segment.htm" class="com.basistech.rseharvest.SegmentFormController">
<property name="sessionForm" value="true"/>
<property name="commandName" value="segment"/>
<property name="commandClass" value="com.basistech.rseharvest.Segment"/>
<property name="validator">
<bean class="com.basistech.rseharvest.SegmentValidator"/>
</property>
<property name="formView" value="segment"/>
<property name="successView" value="showSegmented"/>
<property name="segmenter" ref="segmenter"/>
</bean>
<!-- the page to enter text -->
<bean name="/showSegmented.htm" class="com.basistech.rseharvest.AcceptCorrectedFormController">
<property name="sessionForm" value="true"/>
<property name="commandName" value="acceptCorrected"/>
<property name="commandClass" value="com.basistech.rseharvest.AcceptCorrected"/>
<property name="validator">
<bean class="com.basistech.rseharvest.CorrectionsValidator"/>
</property>
<property name="formView" value="showSegmented"/>
<property name="successView" value="segment"/>
<property name="data" ref="data"/>
</bean>
The onSubmit of the first form returns a ModelAndView pointing to the second form, by naming the same view as you see as the formView of the second form.
Can you explain a little further what this means? Is the "segment" view rendering HTML that contains a <form> that POSTs to /showSegmented.htm?
formBackingObject() is only called on the initial GET request to the page.
Do you want /segmented.htm to display the form and /showSegmented.htm to handle the processing of the input values?
If so, this isn't really the intended usage of AbstractFormController. This class is intended to be used in situations where you want the same controller to handle 1) the presentation of the form and 2) the processing of it's submission.
Hours of messing around seem to have left me with an answer. I'm not sure it's perfect, but it might help other people. There are tons of people frustrated out there with the error message at the top of this question.
In Spring 2.5.6, there is either a bug or a strange limitation, as follows. If a SimpleFormController specifies, as its successView, another page with a SimpleFormController, the second one will fail as in the question. The post data from the first form seems to interfere with what happens on the second form. There seems to be an assumption that the next view after a form is submitted is always a non-form.
If the first page uses a RedirectView to hand off to the second page, all is well.
There are many conflicting pieces of advice, in some cases coming from conflicting heavy-hitters at on the Spring forums, about this. All I know is I made the error posted in the question go away by switching to a redirect. Now the browser does a plain GET of the page for the successView, and the model sets up correctly, and all is well.
Note in passing that the argument to RedirectView is the 'outside' URL (in the tutorial, something like blah.htm) and not the simple token that the mapper maps to JSP pages.
I have a basic Java EE Spring (MVC) application setup that displays a home page with dynamic content. I am completely new to Spring and am confused how to proceed at this point and add more pages to my application. Do I need to create a new controller for each url on my site? Right now I have the following mapping in my ..-servlet.xml file:
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/index.html=homeController
</value>
</property>
</bean>
So if I now have a new page at /login, would I add a mapping to /login/index.html? I get even more confused because I am trying to integrate Spring-security to handle the login page...
I would take a look at annotated Controllers:
http://static.springsource.org/spring/docs/2.5.6/reference/mvc.html
http://static.springsource.org/spring/docs/2.5.6/api/org/springframework/web/bind/annotation/RequestMapping.html
Example:
#Controller
public class TestController {
#RequestMapping(value="/login/index.html")
public String login() {
return "login";
}
#RequestMapping(value="/somethingelse/index.html")
public String login() {
return "somethingelse";
}
}
When you set up your View Resolver, the Strings that are returned would correspond to to a literal page, i.e. somethingelse could be directed to /jsp/somethingelse.jsp if that's how you've set up the resolver in your Spring config. Hint...you need to scan for annotations to auto wire.
Spring-Security is handled in a somewhat similar fashion, but has nothing to do with Spring MVC per say. If done correctly, the only resource you need to provide in order to configure security is the simple login page, which you would configure in your Spring config. Check out this security example:
http://www.mularien.com/blog/2008/07/07/5-minute-guide-to-spring-security/
If you are using spring security, you don't need a controller for showing the login form. You can use any jsp page for that purpose and as spring posts it to j_spring_secutity_check, you don't need a controller to handle it too. Check the spring documentation how you can add multiple methods in controller, You may need to use the beanNmaeMapping kind of configuration. Also the better way now is using annotation based config, which helps you configure any pojo as controller with #Controller annotation
You could use something like:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/vehicleSearch">vehicleSearchController</prop>
</props>
</property>
</bean>
If there was a file /WEB-INF/jsp/vehicleSearch.jsp it would be mapped to the vehicleSearchController. In this case JSP files are being used for the view but you could adapt it to your view technology.
Configuring it this way you would still need to write a mapping for every file. A better way (as Teja suggested) is probably to annotate the mappings in your controller and do away with the XML configuration.
e.g.
#Controller
#RequestMapping("/vehicleSearch")
public class VehicleSearchController {