简体   繁体   English

Spring如何将jsp的url映射到其各自的控制器?

[英]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 我正在一个Spring Web项目上工作,我有很多JSP文件和很多控制器,但是我不知道如何做到这一点。

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

form is automatically mapped to the respective controller in which the updateCustomer is defined. 表单自动映射到在其中定义updateCustomer的各个控制器。 There are other controllers also but how exactly the url updateCustomer goes to respective controller. 还有其他控制器,但是url updateCustomer如何准确地传递给相应的控制器。

The Customer.jsp file is as follows : Customer.jsp文件如下:

<%@ 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 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 : 现在,当我需要某个其他控制器的按钮时,我需要给出整个URL,如下所示:

${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: 在应用程序中找到jsp的属性的原因。

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 您必须按名称校准的表单,方法被八义映射到url或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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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