2

I have started with a simple form submission example

Below are the files

spring-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:

@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

My jsp page is found in

tradelc\src\main\webapp\jsp\testlink.jsp

tradelc\src\main\webapp\jsp\blank.jsp

When I give http://localhost:8181/tradelc/jsp/testlink.jsp, my testlink pages loads. But when I click on submit link, nothing happens after the js alert

9
  • Hint: we can't access your hard drive. Commented Mar 26, 2015 at 12:51
  • downvote?? for what reason? my question got submitted accidentally. now i have edited. pls dont downvote Commented Mar 26, 2015 at 12:53
  • Why do you think the link should do something other than displaying an alert? Commented Mar 26, 2015 at 12:54
  • Because i have given <form:form action="/ClsSave.jsp". so i expected it would submit the form Commented Mar 26, 2015 at 12:57
  • Why do you think a link submits a form? It doesn't. An input or button of type submit submits a form. Not a link. Commented Mar 26, 2015 at 12:59

2 Answers 2

1

I got this working by changing my web.xml. Earlier it was

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

God only knows why I gave *.do here

I then changed it to the following and it worked

<servlet-mapping>
<servlet-name>tradelc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Sign up to request clarification or add additional context in comments.

Comments

0

Get rid of .jsp on your form action attribute.

The action should match your controller @RequestMapping that you want to submit to.

Make your URLs all lowercase. Use - to separate words. Look at the URL for this page for example.

Add <button type="submit">Save</button> inside your form. If you want it to look like a link use Bootstrap and add class="btn btn-link" . Some users disable JavaScript for security, so a button is better than a link.

You should keep your JSPs in /webapp/WEB-INF as its safer.

You should use CSS for layout, not tables, as its faster to render and easier to change. See http://getbootstrap.com/css/#grid

Some form best practices: Spring MVC: Validation, Post-Redirect-Get, Partial Updates, Optimistic Concurrency, Field Security

3 Comments

Even if I remove the .jsp it is not working. I need to work only with links, and not buttons. Pls dont ask why, this is my project structure. I did not keep my jsps inside WEB-INF because I had tough time accessing it with <a:href>
I sometimes see .html or .jsp appended to the form action attribute like in this example and sometimes i dont see like in this example . What is the difference?
@vysh generally you want to leave .html off your URLs, as it looks "cleaner" and is shorter to type. Definitely leave .jsp off your URLs as your URLs should be stable and not rely on the underlying implementation. w3.org/Provider/Style/URI.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.