Reputation: 166
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> Type: </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> Text: </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>
<a href="javascript:submitPageX();">
Save
</a>
| </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
Upvotes: 2
Views: 5575
Reputation: 166
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>
Upvotes: 1
Reputation: 48236
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
Upvotes: 0