无法返回到所需的URL或JSP视图

时间:2013-10-18 08:28:31

标签: forms jsp spring-mvc

<script type="text/javascript" src="${pageContext.request.contextPath}/resources/js/jquery.validate.min.js"></script>

        <script>
        function setHiddenVal(){
      var goAhead = true;
      var myVal="I am hidden value";                
        document.getElementById("secretValue").value = myVal;
        if (goAhead == true) {
        document.forms["register-form"].submit();
      }
      }
      </script> 
     </head>
     <body>
<!--Main Container Starts here-->
<div class="main_container">
    <div class="header">            
        <div class="right_panel">
            <h2 align="center"><u>User Master</u></h2>                      
                <div class="top-form">
                <div>
                  **<form:form action="/usermaster" modelAttribute="CustomerForm" id="register-form" method="POST">**
                   <table cellspacing="0" cellpadding="0" border="" class="form1">

                        <tr>
                                <td class="label">Name:</td>
                                <td>
                                    <form:input path="firstname"/>
                                </td>
                        </tr>
                        <tr>
                                <td class="label">Password:</td>
                                <td>
                                    <form:input path="password"/>
                                </td>

                        </tr>

                        </tbody>

                    </table>

             <div>
                <table>
                    <tr>
                    <td>&nbsp;</td>
                            <td>
                                <input type="button" class="btn blue px16" value="Search" />
                                <input type="button" name="submit" id="btnsubmit" value="Submit" onclick="setHiddenVal();"/>
                                <input type="button" class="btn blue px16" value="Clear" />                             
                                <input type="button" class="btn blue px16" value="Change Password" />               
                                <input type="button" class="btn blue px16" value="Manage User Notification Profile" />              
                     </td>
                    </tr>
                </table>
                   </div>
                </form:form>


                 </div> 
             </div>                                                
            <div class="clear"></div>
        </div>
    </div>
</div>
</body>
</html>


so above one is my code for jsp and below is the code of controller



    @RequestMapping(value={"/usermaster" }, method = RequestMethod.POST)
      public  final String addUserMaster(@ModelAttribute("CustomerForm") CustomerForm pricing, Map<String, Object> map,
                 Model model,  HttpServletRequest request) {
 System.out.println("the first name is "+pricing.getFirstname());
 System.out.println("the password is "+pricing.getPassword());
  return "usermaster";
           }

@RequestMapping(value={"/showusermaster" }, method = RequestMethod.GET)
      public String showPage(ModelMap model){
      model.addAttribute("CustomerForm", new CustomerForm());
      return "usermaster";
      }

但是我的页面使用带有url的弹出窗口打开了:

C:\Users\ganganshu.s\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\YW6383E8\usermaster

所以它应该像

一样打开
http://localhost:8080/enbee/usermaster

你能不能告诉我应该把什么放在表格动作中...因为我认为在弹簧MVC中的表单动作中存在一些错误我们把动作放在上面提到的情况中。

Spring confg文件如下:
                    

    <mvc:interceptors> 
<bean class="com.enbee.admin.interceptor.AuthenticationInterceptor" />

<!-- Declare a view resolver-->
<bean id="jspViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="1" />

并且jsp名称是usermaster.jsp

在sidemenu.jsp中我改为:

 <li><a href="<c:url value='/showusermaster'/>">User Master</a></li>

2 个答案:

答案 0 :(得分:0)

method注释的RequestMapping参数更改为RequestMethod.POST

@RequestMapping(value="/usermaster", method = RequestMethod.POST)
public final String addUserMaster(...){
...
}

因此,当您使用/usermaster将表单提交到网址method="POST"时,此方法将会执行。

您还需要有一个方法(映射到URL),该方法将向用户显示此页面。您可以使用以下方法:

@RequestMapping(value = "/showusermaster", method = RequestMethod.GET)
public String showPage(ModelMap model){
model.addAttribute("CustomerForm", new CustomerForm());
return "usermaster";
} 

使用此方法,URL

http://localhost:8080/enbee/showusermaster

将向用户显示usermaster.jsp页面。现在,当您提交此form时,将调用上述addUserMaster方法。

您不必创建新的jsp文件。网址/showusermaster会将usermaster.jsp返回给用户,用户可以在其中添加表单值并提交表单:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
...
<c:url var="submitUrl" value="/usermaster">
<form:form id="form" action="${submitUrl}" modelAttribute="CustomerForm" method="POST">

现在,当用户点击提交按钮时,此表单将提交到/usermaster网址,并将由addUserMaster方法处理。

答案 1 :(得分:0)

尝试指定控制器方法返回的内容类型,将produces = "text/html" param添加到@RequestMapping注释中。