Possibility to use <c:import> with spring views - java

I was wondering if there is a possibility that I could use the JSTL function to import spring Views.
so something likes this:
<div id="contentHolder">
<c:import url="/foo.do?bar" />
</div>
In the XML stands:
<!-- Viewresolver -->
<b:bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<b:property name="viewClass"><b:value>org.springframework.web.servlet.view.JstlView</b:value></b:property>
<b:property name="prefix"><b:value>/WEB-INF/jsp/</b:value></b:property>
<b:property name="suffix"><b:value>.jsp</b:value></b:property>
</b:bean>
but with on this way I get the error:
Circular view path [/WEB-INF/jsp/foo/index.jsp]: already dispatched to this view path within the same request [/BAR/WEB-INF/jsp/foo/index.jsp]. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
the following manner also doesn't work:
<jsp:include page= and <%#include file=
I guess this doesn't work because of the Viewresolver, but is there a way I can use it this way instead of using JavaScript by example.

you can try full path of the url

Related

No mapping found for HTTP request with URI [/springmvc2k/WEB-INF/views/home.jsp]

I have a Spring project. When I call the URL it is calling the controller and printing the result in console. But the requestmapping method prints the output but not displaying the view / jsp page.
Please find the below link for better understanding.
My Spring Project Structure:
the controller is called and printing 'IN' as output:
The controller i been using.
#Controller
public class BaseController {
#RequestMapping(value="/")
public String getRespnse(Model m) {
System.out.println("IN");
return "home";
}
}
This the view resolver I'm using.
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
I cannot able view the jsp output in browser.Please suggest a way to handle.
If you map your DispatcherServlet to url pattern /* in your web.xml it overrides all other url mappings specified. To handle JSPs web containers includes a mapping *.jsp to handle JSPs.
So you mapping overrides the container`s *.jsp servlet and requests for servlets are now going through your DispatcherServlet which doesnt know how to handle JSPs.
Change your url-pattern from /* to /

Spring page redirection good habits

I'm a beginner with Spring and I'm trying to understand how a controller works.
I have several methods annotated with #RequestMapping and everything works fine, but I don't know how to handle simple page requests.
Suppose I have to redirect from one page to another, without making server logic. If I create the url redirecting to PageA.jsp it seems that I always need to define the method catching the request in the Controller, declaring a method that is pratically empty, like this:
#RequestMapping(value="/PageA.jsp")
public String redirectToPageA(){
return "PageA";
}
If I don't follow this approach I get 404 error as the controller can't find the mapping. I don't like this approach very much as it fills my controller with useless empty methods.
Which is the suggested approach?
I suggest using a view resolver.
Read more about it here - 17.5.1 Resolving views with the ViewResolver interface from 17. Web MVC framework.
In that section you have
As an example with JSP as a view technology, you can use the
UrlBasedViewResolver. This view resolver translates a view name to a
URL and hands the request over to the RequestDispatcher to render the
view
<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>
When returning test as a logical view name, this view resolver
forwards the request to the RequestDispatcher that will send the
request to /WEB-INF/jsp/test.jsp. including the code.
And this answer has the annotation based configuration of a view resolver (except you could use the UrlBasedViewResolver).

Is there a way to configure Roles in a property file for Spring security MVC app

I have a spring security MVC application. In few JSP files I have code that looks like this:
<sec:authorize access="hasAnyRole('ROLE_FOO', 'ROLE_BAR')">
<!--do something here-->
</sec:authorize>
I have to make code changes (changing ROLE_FOO to something else) when deploying the app to production because it has different role names. So I was wondering whether there is a way to configure these role names in a property file and then pick those inside the <sec:authorize> tag.
So the code would look something like this:
Property File:
Admin_Roles = ROLE_FOO ROLE_BAR
and JSP
<sec:authorize access="hasAnyRole(<get roles from Admin_Roles in prop file>)">
<!--do something here-->
</sec:authorize>
btw, I am using Active Directory for authentication so these roles are pre-configured in the active directory for test and production.
Not sure it is the easiest way. But you can write your own expression.
This link should be very useful. link
Since there are some differences between each version. You had better take a look at the source code of DefaultWebSecurityExpressionHandler to make sure you do not miss anything when override createSecurityExpressionRoot
Not sure how you would do this for a variable number of roles, but for a fixed number have you tried something like this?
JSP:
<sec:authorize access="hasAnyRole('${adminRole1}', '${adminRole2}')">
<!--do something here-->
</sec:authorize>
Controller:
#Value("#{myprops.admin_role_1}"}
private String adminRole1;
#Value("#{myprops.admin_role_2}"}
private String adminRole2;
...
#RequestMapping("/hello")
public String hello(final Model model) {
model.addAttribute("adminRole1", adminRole1);
model.addAttribute("adminRole2", adminRole2);
...
}
and config XML:
<bean id="myprops"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<!-- External property files -->
<value>file:${somepathvar}/adminroles.properties</value>
</list>
</property>
</bean>

Difference between indexcontroller and viewresolver in spring

Im newbie to Spring framework. I started learning few basic things from this link. I was stuck with this topic. What is the difference b/w View and Index. Its not much clear in tat doc
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
You see that indexController extends PrameterizedViewController which is a basic controller that always returns a named view. The advantage of this controller is that the client is not exposed to the concrete view technology but only to the controller url.
ViewResolver is of type InternalViewResolver, this means it will resolve views such as jsp, servlet etc .
So you would use viewResolver to link to another jsp or servlet and the indexController would be used in order for your request to be processed by a #Controller class.

How to I define and get locale-based messages in Spring MVC?

I want to define a set of error messages so that when validation errors generate codes, those codes pick up the corresponding error message and print them.
For the sake of learning, and to develop an extendable web app, I'd like to follow the proper i18n path, though I only need to define one (english) set of messages now.
So the locales should all default to english when they don't find their own resources (which I'm yet to define).
I've never used any of the i18n functionality of Java. And the spring docs assume that I have this knowledge.
Could someone just give me a gental nudge in the right direction?
I've defined a messageSource in my dispatcher-servlet.xml webapp context. I have a validator which produces a BindingResult object with a rejected field "username", with code "username.taken". I can generate the default message.
Now I just need to get the error message from the errormessages.properties file in my view.
How do I resolve an error code to a message?
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>errormessages</value>
</list>
</property>
</bean>
It depends on what you want to do with this text. The first possibility is to get the message programmatically:
#Autowired
private MessageSource messageSource;
#RenderMapping(params = "render=details")
public String showDetails (Model model, Locale locale) {
messageSource.getMessage(<your key goes here>, null, locale);
}
This way is very uncommon, cause you have to get the message keys form the Errors object by yourself.
Another more common way is to use the build in view extensions shipped with spring mvc. You didn't wrote it but I guess your using JSPs. In that case you can simply write something like this in your JSP:
<!-- showing errors -->
<div>
<form:errors path="*" />
</div>
<!-- showing arbitrary messages -->
<div>
<spring:message code="${success.messageKey}"/>
</div>
For further reading I suggest you http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/view.html

Categories