表格提交行动不在春季工作

时间:2015-03-26 12:46:24

标签: jsp spring-mvc

我已经开始使用简单的表单提交示例

以下是文件

弹簧servlet.xml中

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xmlns:context="http://www.springframework.org/schema/context"

   xmlns:p="http://www.springframework.org/schema/p"

   xmlns:mvc="http://www.springframework.org/schema/mvc"

   xsi:schemaLocation="http://www.springframework.org/schema/beans

          http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

          http://www.springframework.org/schema/context

          http://www.springframework.org/schema/context/spring-context-4.0.xsd

          http://www.springframework.org/schema/mvc

          http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">



   <context:annotation-config />

   <!-- Scans within the base package of the application for @Components to configure-->

   <context:component-scan base-package="com.bankofny.inx.omx.lc.web" />



   <!-- Enables the Spring MVC @Controller programming model -->

   <mvc:annotation-driven />



   <bean id="messageSource"

          class="org.springframework.context.support.ResourceBundleMessageSource">

          <property name="basename" value="resources.application" />

   </bean>





   <bean id="viewResolver"

          class="org.springframework.web.servlet.view.InternalResourceViewResolver">

          <property name="viewClass"

                 value="org.springframework.web.servlet.view.JstlView"></property>

          <property name="prefix" value="/jsp/"></property>

          <property name="suffix" value=".jsp"></property>

   </bean>

testlink.jsp:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="logic" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="/WEB-INF/tlc.tld" prefix="tlc" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<form:form action="/ClsSave.jsp" method="POST" >
<table>
<tr>
<td align=right valign=top>&nbsp;&nbsp;&nbsp;&nbsp;Type:&nbsp;</td>
                <td align=left valign=top>
                   <select id="TypeCode" name="TypeCode">
                                  <option value="idValue"> Conditions 1</option>
                                  <option value="idValue">condition 2</option>
                           </select>

                </td>
                </tr>
                <tr>
                <!-- Text -->            
                <td align=right valign=top>&nbsp;&nbsp;&nbsp;&nbsp;Text:&nbsp;</td>
                <td colspan=3 align=left valign=top>

                       <textarea tabindex="3" rows="20" cols="65" id="Text" name="Text" rows="5" cols="30"></textarea>



              </td>
           </tr>
           <tr>

                           <td>&nbsp;
                                  <a href="javascript:submitPageX();">
                       Save
                    </a>  

                           &nbsp;|&nbsp;</td>
                           </tr>
</table>
</form:form>

<script>
function submitPageX()
{
       alert("submitted");
}
</script>

控制器:

@Controller
@SessionAttributes("clsData")
public class InformLoginAction {


    /**
     * Process the login form
     */

    @ModelAttribute("clsData")
    public ClauseData createBean() {
        return new ClauseData();
    }

    @RequestMapping(value = "/ClsSave", method = RequestMethod.POST)
    public ModelAndView execute(HttpServletRequest  request,
            HttpServletResponse response,
            @ModelAttribute("clsData") ClauseData clauseData,
            BindingResult bindingResult,
            Model model)
    {
       return new ModelAndView("blank");
    }



    @RequestMapping(value = "/informlogin", method = RequestMethod.GET)
    public ModelAndView execute( HttpServletRequest  request,
                                 HttpServletResponse response,
                                 @ModelAttribute("clsData") ClauseData clauseData,
                                 BindingResult bindingResult)
        throws Exception {
.
.
.
.
.
. return modelAndView;

    }
}

blank.jsp

form successfully submitted

我的jsp页面位于

tradelc \ SRC \主\ web应用\ JSP \ testlink.jsp

tradelc \ SRC \主\ web应用\ JSP \ blank.jsp

当我提供http://localhost:8181/tradelc/jsp/testlink.jsp时,我的testlink页面会加载。但是当我点击提交链接时,js警报后没有任何反应

2 个答案:

答案 0 :(得分:0)

删除表单操作属性中的.jsp

action应与您要提交的控制器@RequestMapping相匹配。

将您的网址全部设为小写。 Use - to separate words。例如,查看此页面的URL。

在表单中添加<button type="submit">Save</button>。如果您希望它看起来像链接,请使用Bootstrap并添加class="btn btn-link"。有些用户因安全性而禁用JavaScript,因此按钮优于链接。

您应该将您的JSP保存在/webapp/WEB-INF中,因为它更安全。

您应该使用CSS进行布局,而不是表格,因为渲染速度更快,更容易更改。见http://getbootstrap.com/css/#grid

某些形式的最佳做法:Spring MVC: Validation, Post-Redirect-Get, Partial Updates, Optimistic Concurrency, Field Security

答案 1 :(得分:0)

我通过更改web.xml来实现这一点。早些时候它是

<servlet-mapping>
<servlet-name>tradelc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

上帝只知道我为什么在这里*.do

然后我将其更改为以下内容并且有效

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