I'm using MultiActionCotroller for handling user details curd operations.
In Controller I'v define add, delete, update method and these methods referring different pages.
Every page then invoking different Controller(SimpleFormController) for performing their operation but when I'm trying to invoke different controller, its not going to that controller. Its again searching into the MultiActionController with the action path name of refered page.
Its not getting out from the MultiActionController, here I'm not understanding the behavior of the MultiActionController.
In my example, In the add_details_form.jsp, I'm defined action path name, that is "details_path.htm". and this is suppose to invoke "AddCtrl" controller but it's not going to AddCtrl but UserMultiCtrl.
Really I'm stuck here, I can't move forward.
I'm not familiar with annotation
I'v gone through many sites but I didn't get any...
I'v very high expectation from StackOverFlow of getting solution of it.
Thanks in advance
Here is my code
Index.jsp
<ul>
<li>Add User Details<br>
</li>
<li>Update User Details
</li>
<li>Delete User Details
</li>
</ul>
UserMultiCtrl
public class UserMultiCtrl extends MultiActionController {
public ModelAndView add(HttpServletRequest request,HttpServletResponse response)throws Exception {
ModelAndView mav = new ModelAndView();
mav.setViewName("add_details_form");
return mav;
}
public ModelAndView update(HttpServletRequest request,HttpServletResponse response)throws Exception {
ModelAndView mav = new ModelAndView();
mav.setViewName("update_details_form");
return mav;
}
public ModelAndView delete(HttpServletRequest request,HttpServletResponse response)throws Exception {
ModelAndView mav = new ModelAndView();
mav.setViewName("delete_details_form");
return mav;
}
}
Spring-Servlet.xml
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="/multiaction/*.htm" class="ctrlPack9.UserMultiCtrl">
<property name="methodNameResolver" >
<bean class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver" />
</property>
</bean>
<bean id="/details_path.htm" class="ctrlPack9.AddCtrl">
<property name="commandClass" value="hiberPack9.UserDetails"/>
<property name="commandName" value="userDetails"/>
<property name="formView" value="add_details_form"/>
<property name="successView" value="userDetails_list"/>
</bean>
add_details_form.jsp
<form:form action="details_path.htm" commandName="userDetails" method="POST">
User Name:<input type="text" name="name" /><br/>
Address:<input type="text" name="address" /><br/>
Age:<input type="text" name="age" /><br/>
<input type="submit" value="submit" />
</form:form>
AddCtrl.java
public class AddCtrl extends SimpleFormController {
private DetailsJB djb;
public void setDjb(DetailsJB djb) {
this.djb = djb;
}
#Override
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
UserDetails detail = (UserDetails) command;
djb.store(detail);
List userDetails = djb.retrive();
ModelAndView mav = new ModelAndView();
mav.addObject("userDetails", userDetails);
mav.setViewName("userDetails_list");
System.out.println("in addctrl onSubmit ");
return mav;
}
}
I got the point where is the actual problem.
There is nothing to do with spring-servlet.xml we can do url mapping in any manner.
when any request go to the MultilActionController(UserMultiCtrl) it adds "multiaction" into the url heading that why when I'm try to invoke AddCtrl with action path name details_path.htm. It is appended by the suffix multiaction in the url. So it is searching into the MultilActionController(UserMultiCtrl) and its not getting any appropriate method.
If anyone know, how to resolve this url in url bar then I can get my solution.
Otherwise I've to append multiaction suffix to every action path and I don't want to do.
Scenario 1:
If you do not have handler mapping specified explicitly, dispatcher servlet provides BeanNameUrlHandlerMapping by default. '/' is not a valid character in the "id" attribute of xml bean definitions.
In BeanNameUrlHandlerMapping, mapping is done from URLs to beans with names that start with a slash("/") and not ids. I don't see name attribute specified in your controller definitions.
So change your controller definitions as:
<bean name="/multiaction/*.htm" class="ctrlPack9.UserMultiCtrl">
...
</bean>
<bean name="/details_path.htm" class="ctrlPack9.AddCtrl">
...
</bean>
Scenario 2:
If you do not want to rely on default BeanNameUrlHandlerMapping, define a SimpleUrlHandlerMapping as:
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/multiaction/*.htm=userMultiCtrl
/details_path.htm=addCtrl
</value>
</property>
</bean>
<bean id="userMultiCtrl" name="userMultiCtrl" class="ctrlPack9.UserMultiCtrl">
...
</bean>
<bean id="addCtrl" name="addCtrl" class="ctrlPack9.AddCtrl">
<property name="commandClass" value="hiberPack9.UserDetails"/>
<property name="commandName" value="userDetails"/>
<property name="formView" value="add_details_form"/>
<property name="successView" value="userDetails_list"/>
</bean>
Related
Why I am getting double slash at WEB-INF/view//admin. I am new at Java MVC so please any suggestion, what am I doing wrong?
here is my code `
<bean 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>`
web.xml
<servlet-mapping>
<servlet-name>webapp-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping
Controller -
#Controller
public class ControllerClass {
#RequestMapping(value = "/deck", method = RequestMethod.GET)
public ModelAndView viewDeckPage(ModelMap model, HttpServletRequest request) {
System.out.println("in get method..");
return new ModelAndView("/admin/deck");
}
}
There are 2 slashes because first one is coming from the view resolver which is /WEB-INF/view/ and the second one you are returning as view name which is /admin/deck (slash before admin).
To resolve this you have to return only view name without a prefixed slash i.e admin/deck
Remove the trailing slash so your prefix is /WEB-INF/view
I a newbie in velocity templates with spring. I am using VelocityViewResolver to server vm files. I want to get the $request.getLocale() but unfortunately no request object is null.
I can access request object to the vm file(create_user.vm) which is set into context by controller directly but request object is not available in other vm files like header.vm. I need locale to pick the javascript file based on the locale.
I know I can pass the request object to header.vm or other vms like #parse("header.vm", $request) but I am not satisfied by this approach. Please let me know the right way..
#RequestMapping(value = "createuser", method = RequestMethod.GET)
public ModelAndView createUser(ModelMap model) throws SQLException {
......
.......
ModelAndView courseModelView = new ModelAndView ("create_user");
return courseModelView;
}
My servelt-dispatcher.xml has velocity configuration as fellow.
<!-- Presentation Configuration -->
<bean id="velocityConfig"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/vm/" />
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="true" />
<property name="prefix" value="" />
<property name="suffix" value=".vm" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml">
</property>
</bean>
I would suggest you modify your controller method as:
#RequestMapping(value = "createuser", method = RequestMethod.GET)
public ModelAndView createUser(ModelMap model, HttpServletRequest request) throws SQLException {
......
.......
ModelAndView courseModelView = new ModelAndView ("create_user");
courseModelView.addObject("locale", request.getLocale());
return courseModelView;
}
Then the 'locale' object will be available in your model to be used in the template.
I am not getting the page displayed ,after tiles resolving the page which was redirected by a controller
I have my controller
public ModelAndView addUser(){
if(success){
return new ModelAndView("redirect:myredirectPage.html");
}else {
--show error page---
}
}
and in the same controller
#RequestMapping(value="/myredirectPage", method=RequestMethod.GET)
public ModelAndView showMyRedirectPage(){
ModelAndView modelView = new ModelAndView("redirectPage");
return modelView;
}
all I see in the my log is , tiles is resolving the redirected view, but the page is not getting displayed in the browser.
Added model object 'org.springframework.validatio
n.BindingResult.command' of type
[org.springframework.validation.BeanPropertyBin dingResult] to request
in view with name 'redirectPage' 02 Dec 2013 21:03:23
[http-apr-8080-exec-3] DEBUG [org.springframework.web.servl
et.DispatcherServlet] - - Successfully completed request
and I have spring config file view resolver, where tiles view resolver is given priority.
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="1"></property>
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="TilesviewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="order" value="0"></property>
<property name="viewClass">
<value>
org.springframework.web.servlet.view.tiles2.TilesView
</value>
</property>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
cant able to debug, since I am not getting any error on logs. appreciate yours responses
Are you absolutely sure that the redirected request is hitting your showMyRedirectPage() method? That method is mapped to /myredirectPage but the redirect request is going to myredirectPage.html
Have you tried adding the .html extension to your #RequestMapping
#RequestMapping(value="/myredirectPage.html", method=RequestMethod.GET)
public ModelAndView showMyRedirectPage(){
ModelAndView modelView = new ModelAndView("redirectPage");
return modelView;
}
In my project I use Velocity 1.7 with Spring MVC 3.1. Below there is source code which shows how velocity beans are configured:
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/views/"/>
</bean>
<bean class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="contentType" value="UTF-8" />
<property name="prefix" value="" />
<property name="suffix" value=".vm" />
<property name="exposeSessionAttributes" value="true"/>
<property name="attributes">
<map>
<entry key="authentication">
<bean class="com.myapp.AuthenticatedUserDetails" />
</entry>
</map>
</property>
</bean>
Everywhere I use UTF-8 encoding. When I have a static text in a view encoding works perfectly. The problem is when I want to display a value in my .vm template from an object which is passed from the model.
Above I pass authentication object to every velocity template. When I want to use authentication object in view like for example to display authentication.username value I have an encoding issue - special characters are not correctly displayed. For example inside authentication.username there is a value 'Rafał' but when I pass this value to the view the displayed value is 'Rafa?'.
Does someone have a similiar problem? I would be really grateful for your help.
Here is velocity template code:
#set ($username = $authentication.nameAndSurname)
<div id="menuBox">
<span class="loggedAsLabel">User <b>$!username</b></span>
</div>
Here is how view is returned:
#Controller
public class UserController extends BaseController {
#RequestMapping(value = "user-view")
public String returnView() {
return "user-view";
}
}
And here is getNameAndSurname method:
public String getNameAndSurname() throws UnsupportedEncodingException {
Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (obj instanceof User) {
User user = (User) obj;
return user.getName() + ' ' + user.getSurname();
} else {
return null;
}
}
I know that this is not a problem with how I retrieve data from database, because from another controller I am able to return data from DAO with correct UTF-8 encoding as a JSON response.
I'm trying to use the following format for making a put request through the RESTtemplate.
#Autowired
RestTemplate template;
#RequestMapping(value = "/change", method = RequestMethod.PUT)
public ModelAndView change(Data data){
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.APPLICATION_XML);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(acceptableMediaTypes);
HttpEntity<Data> entity = new HttpEntity<Data>(data, headers);
String url="http://www...com";
try {
template.put(url, entity);
} catch (Exception e) {
System.out.println(e);
}
return new ModelAndView("redirect:/home");
}
I checked on the database and I realized that there is no change. Even the request is not written on the log file. When I'm debugging, I am not getting any error. Probably I'm not using correctly the put method. Can anyone suggest me how should I use the put method or what else should I try to perform a put request with the RestTemplate?
Also i try to use the exchange method instead of the put:
try {
ResponseEntity<Data> result = template.exchange(Url, HttpMethod.PUT, entity, Data.class);
} catch (Exception e) {
System.out.println(e);
}
But in this case i'm taking the following exception:
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [website.Data] and content type [text/html;charset=utf-8].
As you can see from the headers i set the content type as application/xml and not text/html. I look at the headers and i can see that contained:
Accept: application/xml
I really can't understand. What else should i change? Any comments on the exception?
Configuration:
<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver"
p:basename="config/views" p:order="1" />
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location">
<value>/WEB-INF/classes/config/xml-views.xml</value>
</property>
<property name="order" value="0" />
</bean>
<!--It is used for redirect-->
<bean id="urlBasedViewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value=""/>
<property name="suffix" value=""/>
<property name="order" value="2" />
</bean>
<context:annotation-config />
<!--<context:annotation-config />-->
<context:component-scan base-package="data.controller" />
<context:component-scan base-package="data.service" />
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
</list>
</property>
</bean>
I guess the client of your application is a web-page (then HTML). This article explain what to do for your webapp to be ready for the future browsers compatibility (if they decide later to support e.g PUT, DELETE operations).
In summary for our project we just declared these line in the web.xml:
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/app/*</url-pattern>
</filter-mapping>
The article talk about javascript to add (probably for version prior to Spring 3.0 M1), but we found this solution better as it is just a configuration.