OpenMRS module - Form submit with POST method is not working.

Application Name: OpenMRS 2.3.0 with Reference Application 2.10.0

Question: I am using spring MVC. I created two request mappings in the controller for methods GET and POST. I also created a JSP file that has a form which points to the controller’s POST method. But on submitting the form, it shows 404 error with the same mapping.

My controller - NewDepartmentController is:

@Controller("departmentmodule.NewDepartmentController")
@RequestMapping("/module/departmentmodule") // parent url mapping

// some codes here ......

@RequestMapping(value = "/department.form", method = RequestMethod.GET)
public String showAddForm(Model theModel) {
    Department theDepartment = new Department();
    theModel.addAttribute("department", theDepartment);
    
    return "/module/departmentmodule/department-form";
}

@RequestMapping(value = "/department.addForm", method = RequestMethod.POST)	
public String submit() {
	
    System.out.println("hihihih");
    return "redirect:/module/departmentmodule/department.list";
}

department-form.jsp:

<%@ include file="/WEB-INF/template/include.jsp"%>
<%@ include file="/WEB-INF/template/header.jsp"%>
<h2>Add department form</h2>
<br/>
<form action="${pageContext.request.contextPath}/module/departmentmodule/department.addForm" method="POST">
<%-- need to associate this data with customer id --%>
<spring:bind path="department.departmentId">
	<input type="hidden" name="departmentId" value="${status.value}" />
</spring:bind>
<fieldset>
	<table>
	    <tr>
	        <td><openmrs:message code="general.name"/></td>
	        <td>
	            <spring:bind path="department.name">
	                <input type="text" name="name" value="${status.value}" size="35" />
	                <c:if test="${status.errorMessage != ''}"><span class="error">${status.errorMessage}</span></c:if>
	            </spring:bind>
	        </td>
	    </tr>
	    <tr>
	        <td valign="top"><openmrs:message code="general.description"/></td>
	        <td valign="top">
	            <spring:bind path="department.description">
	                <textarea name="description" rows="3" cols="40" onkeypress="return forceMaxLength(this, 1024);" >${status.value}</textarea>
	                <c:if test="${status.errorMessage != ''}"><span class="error">${status.errorMessage}</span></c:if>
	            </spring:bind>
	        </td>
	    </tr>
	</table>
	<br />
	<input type="submit" value="<openmrs:message code="department.save"/>" name="save">
    </fieldset>
</form>
<%@ include file="/WEB-INF/template/footer.jsp"%>

The form is:

And the result after form submit is:

Please help me to solve this issue.

@sam1220 annotate your showAddForm class with the @Controller annotation since spring only detects a class as a controller class if its having the @Controller annotation

Actually, I am using it already. It’s not working.

@sam1220 also try to check fast that when you POST the information its received and captured in the database before you do a GET to retrieve it back. This will required whether you created a database table in the openmrs database to capture your information

Hello, @sam1220, please what local server are you using?

I used to have this problems, when using eclipse and tomcat. If that’s the case with you, try deleting the server and installing again.

I got to know what caused this problem. OpenMRS spring MVC request mapping is not working without .form, .list at last.

Like for example:

  • @RequestMapping("/department") does not work
  • @RequestMapping("/department.showForm") does not work
  • @RequestMapping("/department.form") works
  • @RequestMapping("/department.list") works

Is there any way that we can make the request mappings work without .form or .list at last? Is there anything that I need to understand/do?

@dkayiwa Is this the current intended behavior?

Does it work if you for instance change @RequestMapping(value = "/department.form", method = RequestMethod.GET)

to this?

@RequestMapping(value = "/department-form", method = RequestMethod.GET)