简体   繁体   中英

How the Spring maps url from a jsp to its respective controller?

I am working on a Spring web project , I have many JSP files and many controllers , but I am not able to grab how this

<form:form action="updateCustomer" autocomplete="true"  commandName="customer">  

form is automatically mapped to the respective controller in which the updateCustomer is defined. There are other controllers also but how exactly the url updateCustomer goes to respective controller.

The Customer.jsp file is as follows :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<!DOCTYPE html>
<html lang="en">

<head>
<link
    href="${pageContext.request.contextPath}/static/css/bootstrap-nav-wizard.css"
    rel="stylesheet">
<link
    href="${pageContext.request.contextPath}/static/css/intlTelInput.css"
    rel="stylesheet">

<style>
ul.nav-wizard li a i {
    margin-right: 15px;
}
</style>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script     src="${pageContext.request.contextPath}/static/js/flickity.pkgd.min.js"></script>
<script     src="${pageContext.request.contextPath}/static/js/jquery.fancybox.pack.js"></script>
<script     src="${pageContext.request.contextPath}/static/js/waypoints.min.js"></script>
<script     src="${pageContext.request.contextPath}/static/js/custom/customer.js"></script>
<script     src="${pageContext.request.contextPath}/static/js/jqueryform-validator.js"></script>
<script     src="${pageContext.request.contextPath}/static/js/custom/common.js"></script>

<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/intlTelInput.min.js"></script>
</head>

<body>
    <form:form action="updateCustomer" autocomplete="true"  commandName="customer">
        <form:hidden path="buyerId"/>
        <form:hidden path="user.userId" />
        <section>

            <div class="container" style="margin-top: 10px;">
                <div class="row">
                    <h3 class="main-title">My Profile</h3>
                </div>
                <div class="row">

                    <div>

                        <!-- Main Content Start -->
                        <div id="myTabContent" class="tab-content">
                            <!-- Step 1 Content Start -->
                            <div class="tab-pane fade active in" id="step1">
                                <div class="panel panel-default">
                                    <div class="panel-heading">
                                        <h3 class="panel-title">Personal Details</h3>
                                    </div>
                                    <div class="panel-body">
                                        <div class="row">
                                            <div class="col-xs-12 col-sm-4 form-group">
                                                <label>First Name</label><span class="req">*</span>
                                                <form:input class="form-control" path="user.firstName"
                                                      type="text"  maxlength="75"
                                                    />
                                            </div>
                                            <div class="col-xs-12 col-sm-4 form-group">
                                                <label>Middle Name</label>
                                                <form:input class="form-control" path="user.middleName"   maxlength="75"
                                                    type="text" />
                                            </div>
                                            <div class="col-xs-12 col-sm-4 form-group">
                                                <label>Last Name</label><span class="req">*</span>
                                                <form:input class="form-control" path="user.lastName"
                                                      type="text"   maxlength="75"
                                                     />
                                            </div>
                                        </div>


                                    </div><!--//panel body over -->
                                </div><!--//panel panel default over -->

                                <div class="panel panel-default">
                                    <div class="panel-heading">
                                        <h3 class="panel-title">Company Details</h3>
                                    </div>
                                    <div class="panel-body">
                                        <div class="row">
                                            <div class="col-xs-12 col-sm-6 form-group">
                                                <label>Company Name</label><span class="req">*</span>
                                                <form:input path="companyName" class="form-control"
                                                      type="text"  
                                                    maxlength="45"
                                                     />
                                            </div>
                                        </div>

                                    </div>
                                </div>
                                </div>
                                <div class="row" style="display: none;" id="mainBtn">
                                    <div class="col-xs-6 col-sm-2 pull-right" style="min-width: 170px;">
                                        <button class="btn" type="submit" name="action" value="2" style= "min-width: 170px;">Save & Continue</button>
                                    </div>
                                    <div class="col-xs-6 col-sm-2 pull-right" style="text-align: right; padding-right:0px;">   <!--  added property  padding-right:0px; to style   on 17/7 -->
                                        <button class="btn" type="submit" name="action" value="1" style= "min-width: 170px;">Save</button>
                                    </div>
                                </div>
                                <div class="row"  id="editBtn">
                                    <div class="col-xs-6 col-sm-2 pull-right">
                                        <a class="btn pull-right" id="edit"
                                            href="#" onclick="makeEditable()" style="min-width: 170px;">Edit</a>
                                    </div>
                                </div>
                                <br> <br>
                            </div>
                            <!-- Step 1 Content End -->
                        </div>
                        <!-- Main Content End -->
                    </div>
                </div>
            </div>
            <!-- /container -->
        </section>
    </form:form>

</body>

</html>

The controller File is as follows :

    package com.htss.web.controller;

//assume all imports

@Controller
@RequestMapping("/buyer")
public class BuyerController {


    @Autowired
    private BuyerService customerService;

    @Autowired
    private UserService userService;

    @Autowired
    private CommonService commonService;
    @Autowired
    private MessageSource messageSource;

    @RequestMapping(value = "/open/customer")
    public String customerInfo() {
        return "customer";
    }



    @RequestMapping(value = "/edit_profile")
    public String editCustomerProfile(HttpSession session, Model model) {
        Integer buyerId = (Integer) session.getAttribute("entityId");
        BuyerFormBean bean = customerService.retrieveCustomer(buyerId);
        Long userId = (Long) session.getAttribute("userId");
        try {
            UserFormBean user = userService.getUser(userId);
            bean.setUser(user);
        } catch (IllegalAccessException | InvocationTargetException e) {
        }
        model.addAttribute("customer", bean);
        model.addAttribute("countries", commonService.getCountryDropdown());
        model.addAttribute("action", "updateCustomer");
        return "buyerProfile";
    }



    @RequestMapping(value = "/updateCustomer")
    public String updateCustomerProfile(Model model, HttpSession session, BuyerFormBean customer) {

        try {
            if (action == 1 || action == 2) {
                customer = customerService.modifyCustomer(customer);

            }

            }
            catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error",messageSource.getMessage("msg.Error",null,Locale.US));
        }
        Integer buyerId = (Integer) session.getAttribute("entityId");
        BuyerFormBean bean = customerService.retrieveCustomer(buyerId);
        Long userId = (Long) session.getAttribute("userId");        
        try {
            UserFormBean user = userService.getUser(userId);
            bean.setUser(user);
        } catch (IllegalAccessException | InvocationTargetException e) {
        }

        model.addAttribute("customer", bean);
        model.addAttribute("message",messageSource.getMessage("msg.Success",null,Locale.US));
        return "Customer";
    }


}

Now the question is when I click save button the url formed is :
http://localhost:8080/82ism/buyer/updateCustomer

How this happened ? and now when I need a button to some other controller I need to give the whole URL as follows :

${pageContext.request.contextPath}/seller/edit_profile

The project is working all fine I am just trying to understand this concept.

The whole point of spring is so you don't have to worry about that stuff. jsp's get found cause of the propperty's in application.propperties: like:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

For the forms and methods... It's not like it magicly happens. A form you have to cal by name and methods are eighter mapped to a url or action

like:

@RequestMapping("/")
or
@RequestMapping(method=RequestMethod.POST)

To call the values of a form from the controller you first have to bind them to a entity model with the fields of the form as variables. The method would look like:

@RequestMapping(method = RequestMethod.POST)    
public String processRegister(@ModelAttribute("userForm") User user,   
Map<String, Object> model) {
    ...
    return "view";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM