la redirección de primavera no cambia la URL del navegador a la URL adecuada
Frecuentes
Visto 3,260 equipos
2
Mi controlador es el siguiente
@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";
}
}
mapeo de servlet en 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
ha incluido el 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.
3 Respuestas
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
podemos agregar data-ajax=false
on each form tag.
contestado el 29 de mayo de 14 a las 11:05
0
Will this be helpful? Spring MVC Controller redirige usando parámetros de URL en lugar de en respuesta
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"
en su lugar?
en realidad, el 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
a 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
contestado el 23 de mayo de 17 a las 12:05
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.
Respondido 02 Jul 16, 03:07
Please refrain from answers that just say "thank you". Several such answers may lock you from posting answers altogether. - grochmal
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas spring jquery-mobile or haz tu propia pregunta.
return "redirect: /search"
ereturn "redirect: users/search"
wont work as my controller is mapped to/users
. Sin embargo,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 Molabantiwell, you are right, it's redirect. I've run some tests, and got something above. - ROROROOROROR
Please check my code in question properly. I used ` redirect:/users/search`. I think I used the right one only. - Raghu Molabanti
I didn't mean you write something wrong in your redirect. So you mean the thing is,
"redirect:search"
mientras"redirect:/users/search"
remains in the same page? - ROROROOROROROkay, What would be the cause. I have read many post related to this. :( - Raghu Molabanti