i18n in form select (Spring MVC 3.2.8) - spring

I have a project based in Spring Web model-view-controller (MVC) framework. The version of the Spring Web model-view-controller (MVC) framework is 3.2.8.
I want to use a message inside a form:select, but I got an error:
org.springframework.beans.NotReadablePropertyException: Invalid property `'<fmt:message key=' of bean class`
The JSP
<form:select path="cancelledSubStatus"
id="cancelledSubStatusId"
items="${cancelledSubStates}"
itemValue="key"
itemLabel="<fmt:message key="${key}" />"
cssClass="c-select"
/>

outside of the form you can do:
<fmt:message key="your.message" var="ItemLabelMessage" />
so you are saving the message in that variable and then inside of the form select you access that variable by doing:
itemLabel="${ItemLabelMessage}"
There it should work
Hope it works! Good luck

Related

Spring MVC insert URL to another endpoint in the view

I'm migrating a Play! 1.2 web application and moving to Spring Boot + Spring MVC. Some views contain URLs to other endpoints. For example, I display the book title on the page and next to it I want to add the URL to go the book's details page (e.g. localhost/books/{id}).
In Play! 1.2 the controllers are static, and there is also a Router which can create the full URL for a method belonging to another controller (Router.getFullUrl("BookController.bookDetails", args)), but how do I achieve this with Spring MVC?
Best regards,
Cristian.
If you are trying to get the app/deployed name automatically in .jsp files to make the urls, then please make use of context path. An example below :
<c:set var="context" value="${pageContext.request.contextPath}" />
<script src="${context}/themes/js/jquery.js"></script>
From your requirement "admin.myapp.com","admin-test.myapp.com" are server names right? Something like http://admin.myapp.com/book/{bookId},http://admin-test.myapp.com/book/{bookId}. In Spring app, relative path in jsp can be accessed using pageContext.request.contextPath
I also found the UriComponentsBuilder and ServletUriComponentsBuilder. They are similar to the Play! Framework router and provide methods for building URI's, handling parameters and the query etc. We chose to annotate the controllers' methods using constants and then use the same constants with the UriComponentsBuilder to build back the path and create the request query for GET requests.

Is it a good design to use a spring's MVC tag ( let's say form:input tag) within a custom myCustomTag.tag file implementation?

I am developing a myCustomTag.tag file to implement my custom tag in spring MVC supported project. I know that the developer using the myCustomTag would use the myCustomTag within the spring provided tag. For E.g. :
<form:form action="..." commandName="...>
<myprefix:mycustomTag ............ />
</form:form>
Is it a good design to use a spring's MVC tag ( let's say form:input tag) within a custom myCustomTag.tag file implementation ?
Absolutely, I don't see a reason why you would not want to use spring tags within a custom tag.
For example, for a large form, you might want to separate sections into their own tags and spring tags are perfectly ok to be used there (ie, if it is syntactically allowed).

Display Spring MVC model attributes in thymeleaf template

I am developing a full Spring application with Spring MVC and Thymeleaf in view layer. In the past I've worked with JSPs and Spring MVC in view layer, but those are now dinosaurs I guess.
So my problem is that with JSPs I could very easily display model attributes in view by adding value in model.addAttribute in controller and displaying the same in JSP anywhere with placeholder evaluating to springex ${value}. So if I want to place a title in page I can write <title>${appName}<title>. This is one of the places where I can put any springex.
I am having hard time to figure out how to do this with Thymeleaf as it uses attribute based parsers. So anywhere on page if thymeleaf prefix is not included it won't process spring expression. It's very hard to work with limited set of tag libraries. I've heard of custom attributes for thymeleaf but I guess there should be a better way to do this.
You can use the th:text attribute, e.g.
<html ... xmlns:th="http://www.thymeleaf.org">
...
<title th:text="${appName}">mocking text</title>
...
</html>
The content of the tag ("mocking text" in this case) gets replaced by the result of the expression in the th:text attribute.
Of course you need to have the appropriate JAR files on CLASSPATH and have the Thymeleaf view resolver properly configured, as described in the Thymeleaf+Spring guide.
For additional information about how template processing works with Thymeleaf in general you can refer to the Thymeleaf guide.

AJAX validation not working with struts2 Jquery plugin

I am writing a web application with struts2 + Jquery plugin.
I am using AJAX form submission with sj:submit tag from jquery plugin.
I am facing an issue while validating the form (AJAX) with "validate= true" tag in sj:submit.
Following is the sj:submit tag:
<sj:submit value="ADDUSERNOW" onBeforeTopics="before" onSuccessTopics="success"
onErrorTopics="errorState" onCompleteTopics="complete" targets="userMgmntDiv" validate="true"/>
My html head section also contains references to two js files required for validation as below.
<script language="JavaScript" src="${pageContext.request.contextPath}/struts/utils.js" type="text/javascript"></script>
<script language="JavaScript" src="${pageContext.request.contextPath}/struts/xhtml/validation.js" type="text/javascript"></script>
Validation xml is given below which is named as ActionClass-validation.xml and is placed in the same package as that of a class on which validation is invoked:
<validators>
<field name="userModel.userName">
<field-validator type="requiredstring">
<message>UserName Is Required</message>
</field-validator>
</field>
</validators>
After form is submitted , no validation is triggered , and execution proceeds and struts2 action class is called.
Is there anything I am missing here ? I have searched a lot but couldn't find any answer .Please help
struts2-core version : 2.3.4
struts2-jquery-plugin :3.3.3
Action configuration looks loke below :
<action class="addUserAction" method="addUser">
<interceptor-ref name="jsonValidationWorkflowStack"/>
<result>addUser.jsp</result>
</action>
In addition to above,I am using spring + struts2 + hibernate for achieving above task.When I tried the example as shown in struts2-jason wiki page : ,I am getting error as : cannot find action with class named AddUserAction(which is a bean in spring-beans.xml)
It seems that error is thrown when validator inteceptor is invoked.
When I comment out the annotations used for validation on the action class - AddUserAction, this error vanishes and page loads successfully.
Following are my questions :
1)How can I use spring alongwith struts2 convention plugin ? This is because , convention plugin uses its own mechanism for mapping urls and action (using annotations or default behaviour).
2)Previuosly , I was expecting struts.xml file can be used for url to action mapping , and annotations can be used for validations. (using convention plugin)But this approach doesn't seem to work.(convention plugin + struts.xml doesn't work)
struts2 ajax validation is a client side validation, which provided natively by dojo, here is the doc for Struts2 Ajax validation. struts2-jquery plugin provides this feature using jsonValidationWorkflowStack, you can use it like this: Example
I've tried. It's work very well. But if your form has an input file for upload, the ajax validation don't work.

Cannot access struts action data from nested JSP

I am working for first time with Struts 2 + Spring + Hibernate architecture. I took this project as reference for building it and it works. I can list DB tables in my index.jsp using struts tags, but this does not work from nested JSP loaded into DIV containers inside index.jsp.
index.jsp has a <div class="art-nav"></div> and loads there another jsp using js:
$(".art-nav").load("./menu.jsp");
The same struts tags that work in index.jsp to list DB tables do not work in menu.jsp. I am not sure if the problem is the way I am loading this JSP or if it is necessary to execute some action from Struts 2 before loading menu.jsp...
Basically the JSPs use AJAX and I am adapting them to this architecture and there is where I am facing the problems because of my lack of experience.
Thank you in advance for your help!
You should include the second jsp as follows:
<div class="art-nav">
<%# include file="/WEB-INF/jsp/menu.jsp"%>
</div>
If you load your jsp via javascript the response will not get forwarded to that jsp.
If you really want to load a jsp via javascript you should create a new action (menu.action) that returns a parsed jsp and include it in the existing html. (Though I personally do not really like that technique.)

Resources