2

My controller is as follows

@Controller
@RequestMapping(value="/users")
@SessionAttributes("searchUsersDTO")
public class UserController {

@Autowired
private UserService usersService; 

@RequestMapping
public String users(Model model){
    model.addAttribute("searchUsersDTO", new SearchUsersDTO());
    return "search_for_users_page";
}

@RequestMapping(value="/search",method=RequestMethod.GET)
public String search(@ModelAttribute SearchUsersDTO searchUsersDTO,Model model){

    List<UserDetail> list=usersService.search(searchUsersDTO);

    model.addAttribute("usersList", list);
    return "users_list_page";
}

@RequestMapping(value="/edit",method=RequestMethod.GET)
public String edit(@RequestParam(value="userId") Integer userId,Model model){

    UserDetail detail=usersService.fetchUserDetails(userId);

    model.addAttribute("userDetails", detail);
    return "users_edit_view";
}

@RequestMapping(value="/save",method=RequestMethod.POST)
public String save(UserDetailsDTO userDetailsDTO,Model model){

    usersService.updateUserDetails(userDetailsDTO);
    return "redirect:/users/search";
}   
}

servlet mapping in web.xml

<!-- Processes application requests -->
 <servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <init-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>dev</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Frist I will search for users. It will display list of users.
Then I will choose a user to edit,It will display edit view with all details of the user.

Then modify and save the details, which will call save method and after saving the details, should navigate back to search view.

Every thing is working fine. But after saving browser url is displaying as context/users/save, but i required context/users/search.

I figured out the cause of the issues. Need solution.
My users_edit_view has included the jquery.mobile-1.3.2.min.js and I tried after removing of this lib. Then its working as desired.
Don't know why its causing the issue. And how to solve it.

Raghu Molabanti
  • 317
  • 5
  • 16

3 Answers3

1

The problem (I am excepting ) is with jquery-mobile. I disabled the ajax by adding the following script before the jquery-mobile. Now its working as i desired.

<script type="text/javascript">
 $(document).bind("mobileinit", function () {
    $.mobile.ajaxEnabled = false;
});
</script>
<script src="../js/jquery.mobile-1.3.2.min.js" th:src="@{/cms/js/jquery.mobile-1.3.2.min.js}"></script>

Or
we can add data-ajax=false on each form tag.

Raghu Molabanti
  • 317
  • 5
  • 16
0

Will this be helpful? Spring MVC Controller redirect using URL parameters instead of in response

When you return a view name prefixed with "redirect:", Spring MVC transforms this to a RedirectView object anyway, it just does so with setExposeModelAttributes to true (which I think is an odd value to default to).

what if you just use return "redirect: /search" instead?

actually, the return "redirect: users/search" you used works in my project.

I think maybe you should bind the object to your model before redirect.

List<UserDetail> list=usersService.search(something);
model.addAttribute("usersList", list);
model.addAttribute("userList",userList);

It seem like if you return with a "/" at the beginning, like

return "redirect: /text1/text2"

It will redirect to url context/text1/text2

if you use,

return "redirect: text1"

it will replace the last item in the url, for example, from context/Main/old to context/Main/text1

The only difference between our tests is my controller's Mapping doesn't begin with "/". But even if I change that, it the redirect still works. I have my controller:

@RequestMapping(value = "/Main") or(value = "Main")

both work for

redirect:/Main/index.do    or redirect:index.do
Community
  • 1
  • 1
ROROROOROROR
  • 959
  • 1
  • 14
  • 31
  • `return "redirect: /search" ` and `return "redirect: users/search"` wont work as my controller is mapped to `/users`. But `return "redirect:search"` is working fine. Actually the data is populating properly in view. issues is with the url only. And I think adding the data to model in save does not make sense. It should bind data in search method. – Raghu Molabanti May 28 '14 at 13:09
  • well, you are right, it's redirect. I've run some tests, and got something above. – ROROROOROROR May 28 '14 at 13:28
  • Please check my code in question properly. I used ` redirect:/users/search`. I think I used the right one only. – Raghu Molabanti May 28 '14 at 14:47
  • I didn't mean you write something wrong in your redirect. So you mean the thing is, `"redirect:search"` while `"redirect:/users/search"` remains in the same page? – ROROROOROROR May 28 '14 at 15:32
  • Okay, What would be the cause. I have read many post related to this. :( – Raghu Molabanti May 28 '14 at 15:39
  • And can you post your servlet-mapping in the web.xml? I've know idea why the `"/users/search"` doesn't while `"search"` works, this doesn't make sense :C – ROROROOROROR May 28 '14 at 15:42
  • Updated the question with servlet mapping. And actually in both cases i.e `search` and `/users/search` data is populated correctly. And in both cases wrong URL is displayed in browser. – Raghu Molabanti May 29 '14 at 05:11
0

Even jquery.mobile-1.4.5.min.js induces the same problem and adding data-ajax="false" to the form tag fixes the redirect on the server after POST indeed. Thanks for the tip.

Mike V.C.
  • 1
  • 1
  • Please refrain from answers that just say "thank you". Several such answers may lock you from posting answers altogether. – grochmal Jul 02 '16 at 02:34