1) Using Controller at SingleFileUploadController, gives correct result in jsp and when used RestController instead of Controller in SingleFileUploadController, it is not directing to jsp. Why?
2) Is it possible to use both at same time?
reference:
http://memorynotfound.com/spring-mvc-file-upload-example-validator/
Thanks
Harshal
Because RestController is for controllers who don't forward to views. Their return value is sent as the response body.
Yes, it's possible to have Controllers and RestControllers in the same webapp. If you want some methods of your controller to return views, and some others to return response bodies (i.e. act as in a RestController), then use #Controller, and annotate your "REST" methods with #ResponseBody.
To answer the question in regards to #Controller and #RestController being together.
First controller:
#RestController //specify that this class is a restful controller
#RequestMapping("/api")
public class RestHomeController {
Second Controller
#Controller //specify that this class is a controller
#RequestMapping("/")
public class HomeController {
#Controller tells the api to return ModelAndView Object which contains the name of your view hence the jsp file to view, while #RestController returns serialized response.
No you cannot have them both, controller is either annotated with one of them but as #JB Nizet mentioned you can use #Controller and #ResponseBody to achieve the functionality for #RestController for specific API , anyway this was the trend used since the support for RestController was not there before spring 4.
Related
I need a routing that maps
admin/{PartOfControllerName}/{action}
to the controller:
Admin{PartOfControllerName}Controller
For example map admin/post/new to AdminPostController and its New action.
Thanks.
one way to do it is to decorate action with Route metadata
[Route("admin/post/new")]
public ActionResult New(...)
In Jersey we can inject a list of headers to a model class using #BeanParam and #HeaderParam. Are there any alternatives in Spring MVC to do the same thing?
I know in spring-MVC, we can inject all headers to a map **#RequestHeader Map<String, Object> headers** and extract the fields from there. But I was wondering if I can inject my required headers into a model class.
Thanks in advance.
Replacement for #HeaderParam is Spring MVC's #RequestHeader
The #RequestHeader annotation allows a method parameter to be bound to a request header.
#RequestMapping
public void displayHeaderInfo(#RequestHeader("Accept-Encoding") String encoding,
And #BeanParam you don't need special annotation, just add your bean to method as is:
#RequestMapping
public String displayHeaderInfo (MyBean myBean) {
Every time I call my REST API sending a JSON through PUT, for instance, and there is some different property on it, I got 400 (Bad Request) as a result.
Is there any way to configure spring-mvc to ignore no existent properties when JSON and my Class do not perfectly match?
Here is a sample of a method on my controller:
======
#Transactional
#RequestMapping(method=RequestMethod.PUT, value="/include",
consumes=MediaType.APPLICATION_JSON_VALUE,
produces={MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_XML_VALUE })
public #ResponseBody ResponseEntity<Client>
inserirClienteSemRedeSocial(#RequestBody Client client) {
clientDAO.insert(client);
return new ResponseEntity<Client>(client, HttpStatus.OK);
}
you can add to your Pojo:
#JsonIgnoreProperties(ignoreUnknown=true)
which will ignore unknown fields
javadoc
read this article on SO, and had some clarifying questions.
I put my config.properties under src/main/resources
In spring-servlet.xml config file
I added the following:
<context:property-placeholder location="classpath:config.properties"/>
In my business layer, I am trying to access it via
#Value("${upload.file.path}")
private String uploadFilePath;
Eclipse shows error:
The attribute value is undefined for the annotation type Value
Can i not access the property in the business layer or are property files only read in the controller?
UPDATE::
src/main/java/com.companyname.controllers/homecontroller.java
public String home(Locale locale, Model model) {
MyServiceObject myObj = new MyServiceObject();
System.out.println("Property from my service object: = " + myObj.PropertyValue());
if(myObj.PerformService())
{
///
}
}
src/main/java/com.companyname.services/MyService.java
public class MyServiceObject {
#Value("${db.server.ip}")
private String _dbServerIP;
public String PropertyValue() {
return _dbServerIPaseURL;
}
}
Another site where I found the explanation
Please check that you import Value from org.springframework.beans.factory.annotation package:
import org.springframework.beans.factory.annotation.Value;
also the property placeholder must be declared in the respective context configuration file, in case of controller it's likely Spring dispatcher servlet configuration file.
update You are confusing property-placeholder that post processes bean values, that contain dollar symbol ${<property name>} with Spring expression language container extension that process values containing a hash symbol #{<Spring expression language expression>}, in the link you have shown the latter approach is used.
regarding the instantiation of MyServiceObject myObj
If you want the object to be managed by Spring you should delegate its creation to the container:
if MyServiceObject is a stateless service then it's a singleton with the singleton bean scope, you should register it in your application context, for example with the following xml configuration:
<bean class="my.package.MyServiceObject"/>
and inject it to your controller:
private MyServiceObject myServiceObject;
#Autowired
public void setMyServiceObject(MyServiceObject myServiceObject){
this.myServiceObject = myServiceObject;
}
if many instances of MyServiceObject are required, you can declare it as a bean with some other (non-singleton) bean scope (prototype, or request, for example).
However, as there's only one instance of the controller, you can't merely let the Spring container to autowire MyServiceObject instance to the controller field, because there will be only one field and many instances of MyServiceObject class. You can read about the different approaches(for the different bean scopes) for resolving this issue in the respective section of the documentation.
Here is a method that allows us to fetch whatever values are needed from a property file. This can be in Java code (Controller class) or in a JSP.
Create a property file WEB-INF/classes/messageSource.properties It will be in the classpath and accessible in both controllers and JSTL.
Like any property file, this one consists of a key and a value
For example:
hello=Hello JSTL, Hello Contoller
For the Controllers
Locate the xml file that you use to define your Spring beans. In my case it is named servlet-context.xml. You may need to determine that by looking in your web.xml in cases where you are using the servlet contextConfigLocation property.
Add a new Spring bean definition with the id="messageSource". This bean will be loaded at runtime by Spring with the property file key value pairs. Create the bean with the following properties:
bean id="messageSource"
class = org.springframework.context.support.ReloadableResourceBundleMessageSource
property name="basename" value="WEB-INF/classes/messageSource
In the bean definition file for the controller class (testController) add the messageSource as a property. This will inject the messageSource bean into the controller.
bean id="testController" class="com.app.springhr.TestController"
beans:property name="messageSource" ref="messageSource"
In the controller JAVA class, add the messageSource Spring Bean field and its getters and setters. Note the field type is ReloadableResourceBundleMessageSource.
private org.springframework.context.support.ReloadableResourceBundleMessageSource messageSource;
public org.springframework.context.support.ReloadableResourceBundleMessageSource getMessageSource() {
return messageSource;
}
public void setMessageSource(
org.springframework.context.support.ReloadableResourceBundleMessageSource messageSource) {
this.messageSource = messageSource;
}
In your controller code, you can now fetch any known property value from the bundle.
String propValue = getMessageSource().getMessage("hello", objArray, null);
Using JSTL
Since the property file messageSource.properties is in the classpath, JSTL will be able to find it and fetch the value for the given key.
Add the import of the fmt taglib
taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"
Use the fmt:tag in the JSP to fetch a value from the property file.
Sorry about the pseudo syntax, this editor doesn't seem to render any XML.
fmt:bundle basename="messageSource"
fmt:message key="hello"
Hope this helps others
I'm using spring mvc (3.0) with apache tiles in my project. I have multiple forms in a single page rendered through tiles.
The login form and search form are common to most pages. The “body” in the tile definition keeps changing.
So, as shown below, in all of my mvc controllers I have to explicitly set command object in the corresponding model.
1. model.put("userBO", userBO);
2. model.put("searchBO", searchBO);
Is there a way I can move this part of the code to a common place or a global controller, so that I don’t have to write these two lines in all the controllers that I write?
You can use an interceptor to do this in a postHandle:
public class DefaultModelInterceptor extends HandlerInterceptorAdapter {
#Override
public void postHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler,
final ModelAndView modelAndView) throws Exception {
modelAndView.addObject("userBO", userBO);
modelAndView.addObject("searchBO", searchBO);
super.postHandle(request, response, handler, modelAndView);
}
}
This can then be wired in your spring servlet config:
<mvc:interceptors>
<bean class="my.package.DefaultModelInterceptor"/>
</mvc:interceptors>