I have login.jsp , forgotpass.jsp files in webapps directory, I have 2 more jsp file under WEB-INF/jsp folder.
Now when the user clicks ForgotPassword link on login.jsp page he is redirected to forgotpassword.jsp when the user enters some data, which is read by one of Spring Controller, the spring controller sets an attribute and returns same jsp page.
My remote-servlet.xml file is having the following configuration.
...
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
ForgotPasswordController.java
#RequestMapping(value = "/pass", method =RequestMethod.POST)
public String recoverPassword(#RequestParam String email, ModelMap model){
List<String> emails = usersDao.getEmails();
...
if (!emails.contains(email)) {
model.addAttribute("failMessage", "Password Recovery Failed ! Invalid loginId or EmailId");
}else{
String validEmail = email;
model.addAttribute("successMessage", "A New Password is Sent to your emailId "+ "xxx"+email.substring(email.indexOf('#')-3,email.length()));
}
return "forgotpassword";
}
}
The Problem is
After prefixing and suffixing the configuration properties the return Path will be /myapp-Path/WEB-INF/jsp/forgotpassword.jsp.
But my forgotpassword.jsp is under /webapps/ directory, but Spring is checking it under WEB-INF/jsp/ folder.
Can we add another property with prefix "/" and suffix ".jsp" ? If this is not possible, please suggest me any solution.
If you are so intent of not having the forgotPass.jsp in the same folder, you could always return a ModelAndView, setting the View to a JstlView, or rolling your own for that matter. I have to agree with #funnyfox, however, that it seems silly that you have some requirement that prevents these JSP files from living in the same directory tree.
Related
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.)
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"
Here is my view resolver:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".html"/>
</bean>
I also have a controller that should return hello.html:
#Controller
#RequestMapping("/")
public class IndexController {
#RequestMapping(method = RequestMethod.GET)
public String getHtmlPage() {
return "hello";
}
}
When I access localhost:8080 I get an error message:
WARNING: No mapping found for HTTP request with URI [/WEB-INF/pages/hello.html] in DispatcherServlet with name 'mvc-dispatcher'
Now when I change my suffix value to .jsp, then hello.jsp is returned correctly.
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
hello.jsp and hello.html are in the same folder. Any ideas how to fix this?
EDIT:
For html you don't need view resolvers. Instead just create a folder in your webapp folder.
Call it static for example , then add <mvc:resources mapping="/static/**" location="/static/" /> to your xml.
In controller instead of return "hello"; put return "static/hello.html";
As a complement to answers of How to map requests to HTML file in Spring MVC?, I would say that the InternalResourceViewResolver knows how to render JSP or JSP+JSTL views. You could of course use HTML views - even if it makes little sense(*) - by writing a custom ViewResolver that would be able to render plain HTML pages.
(*) Normally, the controller prepares a model that will be used by the view. In a plain HTML view, you cannot use the model. Normally when you try to do that, you actually needs to redirect to a plain HTML page, not use it like a view.
To redirect to the HTML page, you put it out of the WEB-INF folder, in a place accessible as a resource.
But in your particular need, you are trying to display hello.html for the root URL. The best way to do it is to put it directly at the root of the web application folder atn declare it in the welcome-file-list of the web.xml :
<welcome-file-list>
<welcome-file>hello.html</welcome-file>
</welcome-file-list>
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.
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.