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

vysh
  • 166
  • 1
  • 3
  • 14
  • Hint: we can't access your hard drive. – JB Nizet Mar 26 '15 at 12:51
  • downvote?? for what reason? my question got submitted accidentally. now i have edited. pls dont downvote – vysh Mar 26 '15 at 12:53
  • Why do you think the link should do something other than displaying an alert? – JB Nizet Mar 26 '15 at 12:54
  • Because i have given ` – vysh Mar 26 '15 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. – JB Nizet Mar 26 '15 at 12:59
  • But this is the kind of form which i should work with. I only need a link, not a button. How do i make my link submit this form? – vysh Mar 26 '15 at 13:00
  • Use CSS to make your button look like a link. Or use JavaScript to submit the form. – JB Nizet Mar 26 '15 at 13:02
  • After adding `document.forms[0].submit();` my form is getting submitted. But I am getting requested resource not available error. `message /ClsSave description The requested resource is not available.` – vysh Mar 27 '15 at 04:10
  • My spring-servlet.xml is inside WEB-INF, my jsps inside webapp/jsp and controller inside `src/main/java/[package]/[controller class]`. I am not able to move my jsps inside WEB-INF because of the problem i am facing while accessing them using ``. Does any of these cause a problem? – vysh Mar 27 '15 at 05:11

2 Answers2

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>
vysh
  • 166
  • 1
  • 3
  • 14
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

Community
  • 1
  • 1
Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
  • 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 `` – vysh Mar 27 '15 at 04:12
  • I sometimes see .html or .jsp appended to the form action attribute like in [this](http://viralpatel.net/blogs/spring-3-mvc-handling-forms/) example and sometimes i dont see like in [this example](http://www.tutorialspoint.com/spring/spring_mvc_form_handling_example.htm) . What is the difference? – vysh Mar 27 '15 at 05:01
  • @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. http://www.w3.org/Provider/Style/URI.html – Neil McGuigan Mar 27 '15 at 05:25