I am trying to output an Excel file as the view in Spring Roo 1.0.2
What is the quickest way to do this?
(Do I have to add new mapping etc?) At the moment I am using the default Roo AjaxUrlBasedViewResolver.
Thanks
I don't think there's a specific "Roo" way of doing this, but Spring MVC does have an AbstractExcelView class that uses Apache POI and doesn't cause any problems - I think it's the best you can hope for.
First create a view class that extends AbstractExcelView and implements the buildExcelDocument method:
import org.springframework.web.servlet.view.document.AbstractExcelView;
public class XlsView extends AbstractExcelView {
#Override
protected void buildExcelDocument(
Map<String, Object> model, HSSFWorkbook workbook,
HttpServletRequest request, HttpServletResponse response) throws Exception {
//Apache POI code to set up the HSSFWorkbook goes here
response.setHeader("Content-Disposition", "attachment; filename=\"file.xls\"");
}
}
Then add the following to your webmvc-config.xml. I don't think the position matters, but I have it below the section where my TilesConfigurer is listed:
<bean id="excelViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="order" value="1"/>
<property name="location" value="/WEB-INF/views/views-excel.xml"/>
</bean>
Finally create views-excel.xml with the following in it:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean name="XlsView" class="com.your.package.XlsView"/>
</beans>
Related
I am trying to inject property with value inside my controller by using Spring Framework servlet.xml config file. My controller starts like this
package lv.lu.meetings.portal.mvc.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import lv.lu.meetings.domain.jpa.User;
import lv.lu.meetings.domain.jpa.meeting.Attendance;
import lv.lu.meetings.domain.jpa.meeting.Invite;
import lv.lu.meetings.domain.jpa.meeting.InviteStatus;
import lv.lu.meetings.domain.jpa.meeting.Meeting;
import lv.lu.meetings.domain.jpa.notification.Notification;
import lv.lu.meetings.domain.redis.Friend;
import lv.lu.meetings.interfaces.service.NotificationService;
import lv.lu.meetings.interfaces.service.UserService;
import lv.lu.meetings.portal.mvc.WebConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
* Controller for displaying application home page.
*
* Supports multiple tab views: Main, Friends, Notifications, Meetings.
*/
#Controller
public class HomePageController {
// limit meeting count
// defined in meetings-servlet.xml
private String limit;
public String getLimit() {
return limit;
}
public void setLimit(String limit) {
this.limit = limit;
}
My meetings-servlet.xml file is like this
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring Web application configuration file -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<!-- Support for component autowiring -->
<context:component-scan base-package="lv.lu.meetings"/>
<!-- URL mapping for annotation-based Spring Web MVC controllers -->
<bean id="urlMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="loginInterceptor"/>
</list>
</property>
</bean>
<!-- Limit output data -->
<bean id="HomePageController" class="lv.lu.meetings.portal.mvc.controller.HomePageController">
<property name="limit" value="10" />
</bean>
I'm getting error
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'urlMapping' defined in ServletContext resource [/WEB-INF/meetings-servlet.xml]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Cannot map handler 'HomePageController' to URL path [/home]: There is already handler of type [class lv.lu.meetings.portal.mvc.controller.HomePageController] mapped.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
It's definitely smth easy to configure, but i am new to Java and Spring Framework itself, so help is appreciated. Thanks!
You are currently creating 2 HomePageController beans. One from
<!-- Limit output data -->
<bean id="HomePageController" class="lv.lu.meetings.portal.mvc.controller.HomePageController">
<property name="limit" value="10" />
</bean>
and the other from
<context:component-scan base-package="lv.lu.meetings"/>
and the #Controller annotation on
#Controller
public class HomePageController {
Assuming you have a #RequestMapping handler method in this class, Spring will try to register it twice and fail since you can't have two handlers for the same URL.
Choose one or the other bean. If you want to go the annotation way, you can inject the value directly
#Value("10")
private String limit;
or use a property placeholder.
You can set the property with the #Value annotation reading a value from a property file (or whatever):
#Value("${my.limit}")
private String limit;
You can also exclude the HomePageController from the component scanning:
<context:component-scan base-package="lv.lu.meetings">
<context:exclude-filter type="regex" expression="HomePageController$"/>
</context:component-scan>
I want org.springframework.web.servlet.view.InternalResourceViewResolver to resolve both JSP and HTML pages.
Is that possible?
You can configure an InternalResourceViewResolver something like this:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=""/>
</bean>
Where the WEB-INF/pages folder can contain both jsp and html pages and the suffix property is left empty.
Then in your controller, you can have methods that return html views and methods that return jsp views based on the suffix. For example, if index.html and index.jsp both exist in WEB-INF/pages you can do:
#RequestMapping("/htmlView")
public String renderHtmlView() {
return "index.html";
}
#RequestMapping("/jspView")
public String renderJspView() {
return "index.jsp";
}
However, as html pages are static and require no processing, you'd be better to use the <mvc:resources> tag rather than a view resolver for this type of page. See the docs for more info.
I've added a new subdir within my images folder and cannot get the new images to resolve.
Failed to load resource: ... 404 (Not Found)
http://localhost:8080/mywebapp/content/images/subdir/mysubdirimage.png
My directory structure:
src
-- main
--java
--webapp
--content
--images // <- these resolve
--subdir // <- new subdir...resolve fail for images
I have tried adding the following but does't work:
<mvc:resources mapping="/content/**" location="/content/" />
mvc-dispatcher-servelet.xml:
<mvc:annotation-driven/>
<mvc:default-servlet-handler />
<mvc:resources mapping="/content/**" location="/content/" /> //<-- Added this..no go!
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"><value>/WEB-INF/views/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
You are halfway there with the mvc-dispatcher-servlet line you added, but you need to change it to:
<mvc:resources mapping="/images/**" location="/content/images/" />
Also try changing the method in your controller where you are attempting to access the images to something like:
#RequestMapping(value = "/staticImages", method = RequestMethod.GET)
public String showImage() {
return "/images/subdir/mysubdirimage.png";
}
And finally, with the example above try the URL (as you were doing above):
http://localhost:8080/mywebapp/images/subdir/mysubdirimage.jpg
You should also be able to access the images through the #RequestMapping pattern defined in your controller. For example, using the example I gave you above, you would enter the URL:
http://localhost:8080/mywebapp/staticImages
How do I map the '/' request to a controller? The application's context name is reac and I want to bring up the login page when the context is sent to the server. I have REACController like this:
#Controller
public class REACController {
#RequestMapping(value="/", method=RequestMethod.GET)
public String onLaunch() {
return "LoginPage";
}
}
I face the dreaded 404 error for the context, 'resource /reac/ is not available'. What's the right way to handle requests to '/' through a Controller class?
Are you trying to secure the site? In which case you might be better off hooking up Spring Security into the project and have it manage the URL security for you.
But if you really want to map some view to the / then in your application context you can also add something like this
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!--facilitates mapping of Dispatcher Servlet to /-->
<mvc:default-servlet-handler/>
<mvc:view-controller path="/" view-name="login"/>
</beans>
this will forward the / requests to the login view using parameterizable view controller
Still unsure of the reason why you would do something like this as Filter would be a better option even if you were wanting to do login/security check without Security frameworks.
Edit: here is how you can do it through controller itself
#RequestMapping("/")
#Controller
public class MyController {
#RequestMapping()
public String showMeTheLoginPage() {
return "redirect:/login.html";
}
}
Firstly I would like to say that I am quite new to Spring (in particular the MVC framework), and just trying to understand how everything works so please go easy on me.
I'm playing around with a dummy application that I've created, and I've created a simple login form that users can access via the /login.html bean. The bean definition is as follows:
<bean name="/login.html" class="test.controller.LoginController">
<property name="successView" value="list_messages.html" />
<property name="commandClass" value="test.domain.Login" />
<property name="commandName" value="login" />
</bean>
(the Login class is a simple object containing a username and password field with appropriate getters and setters).
The LoginController class does virtually nothing for now:
public class LoginController extends SimpleFormController
{
#Override
protected ModelAndView onSubmit(Object command, BindException errors) throws Exception
{
return new ModelAndView(new RedirectView(getSuccessView()));
}
}
Now I have one view resolver in my bean definition file, which goes as follows:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
To support my Login form I have a login.jsp file in my jsp directory.
My question is as follows: why does accessing /login.html redirect me to login.jsp? I have not specified a formView property for my form, so how does the view resolver know to redirect me to login.jsp?
Thanks in advance for any help!
Joseph.
When you do not specify The logical view name, Spring relies on DefaultRequestToViewNameTranslator, which is installed by default. So if your request is something like
http://127.0.0.1:8080/app/<LOGICAL_NAME_EXTRACTED_BY_VIEW_NAME_TRANSLATOR_GOES_HERE>.html
Have you seen <LOGICAL_NAME_EXTRACTED_BY_VIEW_NAME_TRANSLATOR> ??? So if your request is
http://127.0.0.1:8080/app/login.html
The logical name extracted by ViewNameTranslator is login which is supplied To viewResolver and Translated To
/jsp/login.jsp
Nothing else