RedirectView with dynamic parameter in url - spring

So, in my controller I have a method with mapping #RequestMapping(value = "/profile-{id}/launch") and at the end of it I want to redirect the flow to other method within same controller with mapping #RequestMapping(value = "/profile-{id}/executions") ...
I am obligated to inject RedirectView bean with xml configuration. So I have something like
<bean name="redirect:executions" class="org.springframework.web.servlet.view.RedirectView">
<property name="url" value="#{resourceService.servletPath}/profile-{id}/executions" />
<property name="contextRelative" value="true" />
</bean>
but how am I going to target specific profile ID, since at the end of first method I have to write return "redirect:executions" cause I am targeting the name of the bean, and it cant be dynamic.
Or I target the url with something like return "redirect:/profile-"+someID+"/executions"

Related

JasperReportsViewResolver and Spring MVC - Set filename

I need help with JasperReports and Spring MVC. I can export everything, but I can't set the filename in the output PDF/Excel that my software exports.
In my dispatcher-servlet I have this bean :
<!-- ViewResolver JasperReports -->
<bean id="jasperViewResolver" class="org.springframework.web.servlet.view.jasperreports.JasperReportsViewResolver">
<property name="prefix" value="classpath:/jasper/" />
<property name="reportDataKey" value="dataSource" />
<property name="suffix" value=".jrxml" />
<property name="viewNames" value="Report_*" />
<property name="viewClass">
<value>org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView</value>
</property>
<property name="order" value="1" />
</bean>
That is the ViewResolver provided by Spring MVC.
I have a function in my BaseController ( abstract controller extended by all the #Controller ) :
protected String exportReport(String reportName, String formatoReport, Model model, JRDataSource dataSource) {
model.addAttribute("dataSource", dataSource);
model.addAttribute("format", formatoReport);
return reportName;
}
So, what I do is simply returning this view name from all my #RequestMapping :
#RequestMapping(..something)
public String functionName(...something else) {
.. do some stuff
return exportReport("Report_docIngresso", EFormatoReport.XLS, model, jrDataSource);
}
This works. The export is perfect, but I didn't find the way to set the filename of the exported pdf/excel, that comes out like the latest part of the URL I called before exporting the report.
I already tried to set in the HttpServletResponse the content-disposition with the filename, but it didn't work.
Thanks a lot,
Marco
Try setting the Content-Disposition HTTP response header:
#RequestMapping(..something)
public String functionName(HttpServletResponse response, ...something else) {
.. do some stuff
String header = "inline; filename=myfile.xls";
response.setHeader("Content-Disposition", header);
return exportReport("Report_docIngresso", EFormatoReport.XLS, model, jrDataSource);
}
(Note I saw just now your concluding comment that you tried without success to set the content disposition header. Well.. I can only say it worked for me in a similar setup.)

default view resolution in spring

I am a spring newbie and have a question about view resolution. I am changing a webapp that I downloaded online and it uses the simple view resolver strategy:
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
and I keep getting 404 errors for view resolution and I am suspecting that it uses some sort of rewriting / filtering mechanism. Is there a log that I can view in tomcat / Spring class that I can override in order to understand what file is Spring trying to look for when resolving an incoming request?
I understand the operation of InternalResourceViewResolver which strips file name extensions. But what if the request does not have an extension? For instance:
#RequestMapping("/foo")
protected ModelMap render() { return new ModelMap(); }
Then what is the view name that will be resolved to in this case?
See this link for log4j integration
Spring-mvc doesn't use any file for request handling, it uses controllers and requestmapping to map the request to a controller and respective method if any.
The InternalResourceViewResolver that you have written Resolves “view name” that is returned from the controller class to a JSP page residing in /WEB-INF/view/ directory.
an example
#Controller
public class SimpleController{
#RequestMapping("/home")
public String homeMapper(Model model) {
return "home";
}
}
here homeMapper method will be called if you try to access "home" and as it returns home the relative jsp to be rendered is "home.jsp" should be present in /WEB-INF/view/
For more information see spring mvc reference or any tutorial.

Can I combine #controller and XML bean mapping in spring?

I currently have a #Controller declared in spring and have a bunch of mappings done like so:
#RequestMapping(value = "foo", method = RequestMethod.GET)
public ModelAndView foo() {
ModelAndView mav = new ModelAndView(
"myjsp");
return mav;
}
However every time I want to add a simple JSP mapping I need to recompile and build a new war and deploy.
This isnt so bad except sometimes other members of the team have requests and it would be easier if they can just go into the test env and create the mapping themselves without having to recompile.
I know that you can do similar mapping using xml but can I do this at the same time that I have the #Controller defined?
Like in the example above how could I define that mapping in XML rather than in java?
or say I needed foo2 to map to myjsp2.jsp
I am using spring MVC 3.2
Look into BeanNameUrlHandlerMapping which allows you specify url patterns for controllers in your configuration. Documentation
Example
<beans>
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name="/editaccount.form" class="org.springframework.web.servlet.mvc.SimpleFormController">
<property name="formView" value="account"/>
<property name="successView" value="account-created"/>
<property name="commandName" value="account"/>
<property name="commandClass" value="samples.Account"/>
</bean>
<beans>

Spring 3 #Controller is not being invoked for Get request

I am attempting to use Spring 3' MVC support for annotated controllers in my web application.
In my application-context.xml, I've added the following:
<mvc:annotation-driven />
<context:component-scan base-package="com.abc.def.etc"/>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
<property name="order" value="1" />
</bean>
My Controller is annotated as follows:
#Controller
#RequestMapping("/optimizerRules")
public class OptimizerRulesController {
private OptimizerRulesService optimizerRulesService;
private static final Log LOG = LogFactory.getLog(OptimizerRulesController.class);
public OptimizerRulesController()
{
LOG.info("Initializing OptimizerRulesController");
}
#RequestMapping(method = RequestMethod.GET)
public ModelAndView getRuleAttributesAndRules(ModelMap model)
{
LOG.info("Entering getRuleAttributesAndRules method");
}
When I start up my application I can see in my logs that the OptimizerRulesController has been initialized. I can also see the following:
Creating instance of bean 'optimizerRulesController'
Initializing OptimizerRulesController
Mapped URL path [/optimizerRules] onto handler 'optimizerRulesController'
However, when I invoke my application using http://localhost:8080/appName/optimizerRules I get a 404 error!
What configuration am I missing here?
Thanks
Spring MVC would normally log that no mapping is found for a particular request at WARN level. Assuming you're not seeing that in your logs and assuming that WARN is enabled, and since you're not seeing your own log statement, it sounds like your request isn't even hitting the Spring MVC DispatcherServlet, which probably means the URL is wrong.
The URL should be http://server:port/war/dispatcherServletMapping/optimizerRules, so your web.xml should tell you the missing path component, if my assumptions are valid.

Spring mvc:mapping path rules

I need to map interceptor for all methods in annotated controller with #RequestMapping(value = "/client")
In mapping I have
<mvc:interceptor>
<mvc:mapping path="/app/client/*"/>
<bean class="com.cci.isa.web.CIPClientHandleInterceptor" />
</mvc:interceptor>
This interceptor called perfectly for urls like:
1. http://host/app/client/clientUpdateForm?clientId=305
But doesn't called for urls like:
2. http://host/app/client/clientUpdateForm/clientId_305 (with slash after method name)
How get it called for second variant?
Thanks a lot.
This question is too old, but maybe this helps somebody.
You should try removing /app, I think it's not necessary and perhaps this is causing the problem.
<mvc:mapping path="/client/**"/>
I think this will achieve what you would like:
<mvc:mapping path="/app/client/**/*"/>
The '/**' suggests any number of directories. When this is used in conjunction with '/*', you have something that looks at an arbitrary folder depth, with an arbitrary file name.
If your controller with
#RequestMapping(value = "/client")
Try
<mvc:mapping path="/client**"/>
Try the above suggestion but change the annotation to
#RequestMapping(value = "/client*")
Although I would use two methods for each of the two URI patterns and pass them to the one common method to do the "stuff"...
#RequestMapping(value = "/app/client/clientUpdateFormat/{clientId}", method = RequestMethod.GET)
public String doItOne(#PathVariable("clientId") String clientId) {
doItCommon(clientId);
and
#RequestMapping(value = "/app/client/clientUpdateFormat", method = RequestMethod.GET)
public String doItTwo(#RequestParam("clientId") String clientId) {
doItCommon(clientId);
My problem was, that we used custom RequestMappingHandlerMapping
<bean name="handlerMapping"
class="utils.web.versioning.MobileVersionRewritingMappingHandler">
<property name="order" value="0"/>
<property name="interceptors">
<list>
...
</list>
</property>
</bean>
XML or code config for CORS or any other MVC properties doesn't affect custom handler mappings.
I could specify cors config for custom handler mapping, but I prefer to remove legacy config and use this to configure interceptors:
<mvc:interceptors>
...
</mvc:interceptors>
Now cors is working and I'm using XML global cors configuration.

Resources