Spring表单什么也没做

时间:2016-01-26 08:50:40

标签: java spring jsp spring-mvc

我正在尝试更新用户设置信息。当我点击提交时,没有任何反应。

这是我的设置控制器

..
import java.lang.reflect.Type; // make sure you add this import

class DataMessageList<T>
{
    public List<DataMessage<T>> list;

    public DataMessageList ()
    {
        list = null;
    }

    public void readJson (JsonReader reader)
    {
        Type TT = new TypeToken<ArrayList< DataMessage<T> >>(){}.getType();

        this.list = new Gson().fromJson(reader, TT);
    }
}

JSP表单

@Secured({ "ROLE_USER", "ROLE_ADMIN" })
@RequestMapping(value = "/settings**", method = RequestMethod.GET)
public ModelAndView settings() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String userName = auth.getName();
    User userInfo = userDAO.getUserInfo(userName);
    Settings settings = userDAO.getSettingsInfo(userInfo.getDetails().getMacAddress());
    List<Plugs> plugs = userInfo.getDetails().getPlugs();
    ModelAndView model = new ModelAndView();
    model.addObject("settings", settings);
    model.addObject("plugs", plugs.get(plugs.size() - 1));
    return model;
}

@Secured({ "ROLE_USER", "ROLE_ADMIN" })
@RequestMapping(value = "/settings", method = RequestMethod.POST)
public ModelAndView settings(@Valid @ModelAttribute("settings") Settings settings, BindingResult result) {
    ModelAndView model = new ModelAndView();
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String userName = auth.getName();
    User userInfo = userDAO.getUserInfo(userName);
    if (result.hasErrors()) {
        model.setViewName("welcome");
    } else {
        model.setViewName("settings");
        userDAO.updateSettings(userInfo);
    }

    return model;
}

这是我的提交按钮

<form:form method="POST" modelAttribute="settings" commandName="settings">

如何让表单做任何事情?它应该更新MySQL数据库中的用户详细信息。如果您想要发布更多代码,我会添加它。

1 个答案:

答案 0 :(得分:0)

您可以尝试在表格标签中添加动作属性,如下所示吗?

<form:form method="POST" modelAttribute="settings" commandName="settings" action="/settings">

如果您的Springframework DispatcherServlet servlet映射的url模式不是&#34; /&#34;在web.xml中,请在&#34; / settings&#34;之前添加该模式。如下:

<form:form method="POST" modelAttribute="settings" commandName="settings" action="{url pattern}/settings">