简体   繁体   中英

Send data between Spring MVC controllers

First I define User class:

public class User implements Serializable{

    private int id;
    private String name;    

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public String getNombre() {
        return name;
    }

    public void setNombre(String name) {
        this.name = name;
    }
}

The application I'm doing is responsible for renaming a user, first define responsible view of choosing the id of the user you want to rename (modifyUser.jsp):

<html>
<body>
    <form:form method="post" commandName="modifyUser">
        <table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5">
            <tr>
                <td align="right" width="20%">Id:</td>
                <td width="20%">
                    <form:input path="id"/>
                </td>
            </tr>
        </table>
        <br>
        <input type="submit" align="center" value="Execute">
    </form:form>
</body>
</html>

The driver is responsible for managing this view is (ModifyUserFormController):

public class ModifyUserFormController extends SimpleFormController {
    protected final Log logger = LogFactory.getLog(getClass());
    private UsuarioManager usuarioManager;

    public ModelAndView onSubmit(Object command)
        throws ServletException {
        int id = ((ModifyUser) command).getId();
        Usuario user = usuarioManager.consultUsuarioDao(id);  
        Map<String, Object> myModel = new HashMap<String, Object>();
        myModel.put("usuario", user);
        return new ModelAndView("showModifyUser", "model" ,myModel);
    }
    protected Object formBackingObject(HttpServletRequest request)
        throws ServletException {

        ModifyUser user = new ModifyUser();
        user.setId(5);
        return user;
    }

    public void setUsuarioManager(UsuarioManager usuarioManager) {
        this.usuarioManager = usuarioManager;
    }
}

It gets the user's name through the id you get from the view. This driver would be responsible for sending the name to amend another relationship view-controller, for editing and storing in DB.

I have a problem here because I do not think I properly defining the view or the controller, because I want the view to show me the current name as the id but only get the field empty.

The view is showModifyUser.jsp

<html>
<head/>
<body>
    <form:form method="post" commandName="showModifyUser">
        <table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5">
            <tr>
                <td align="right" width="20%">Nombre:</td>
                <td width="20%">
                    <form:input path="nombre" value = "${model.usuario.nombre}"/>
                </td>
            </tr>
        </table>
        <br>
        <input type="submit" align="center" value="Execute">
    </form:form>
</body>
</html>

And the controller is showModifyUserFormController:

public class ShowModifyUserFormController extends SimpleFormController {

    protected final Log logger = LogFactory.getLog(getClass());
    private UsuarioManager usuarioManager;

    public ModelAndView onSubmit(Object command)
            throws ServletException {

        String nombre = ((CreateUser) command).getNombre();

        CreateUser user = new CreateUser();
        user.setNombre(nombre);

        usuarioManager.saveUsuarioDao(user);

        return new ModelAndView(new RedirectView(getSuccessView()));

    }

    protected Object formBackingObject(HttpServletRequest request) throws Exception {
        CreateUser user = new CreateUser();      
        return user;
    }

    public void setUsuarioManager(UsuarioManager usuarioManager) {
        this.usuarioManager = usuarioManager;
    }

    public UsuarioManager getUsuarioManager() {
        return usuarioManager;
    }
}

1) Why you not using Spring MVC 3? It is more convinient and simpler than 2.x

2) You can do it with one controller class

The issue that you bring new User object to edit form, that's why you do not see name field populated, you need bring passed object from first controller to your edit form. I am not sure, but you can try in second controller

   protected Object formBackingObject(HttpServletRequest request) throws Exception {
    User user = null;
    if (request.getAttribute("usuario") != null)
        user = (User) request.getAttribute("usuario");
    else
         user = new CreateUser();      
    return user;
}

if this not working you can try:

1) pass Id as parameter and get the user in the second controlled in formBackingObject method

2) put user object into session in first controller and get it in formBackingObject of second controller

3) Use forfward from first controller to second and pass user in request as attribute

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM