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

时间:2017-09-13 11:22:37

标签: java spring jsp spring-mvc

我正在开发一个Spring web项目,我有很多JSP文件和很多控制器,但我无法抓住这个怎么样

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

表单自动映射到定义了 updateCustomer 的相应控制器。还有其他控制器,但url updateCustomer 到具体控制器的确切程度如何。

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>

控制器文件如下:

    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";
    }


}

现在的问题是,当我点击保存按钮时,形成的网址是:
http://localhost:8080/82ism/buyer/updateCustomer

这是怎么回事?现在,当我需要一个其他控制器的按钮时,我需要按如下方式提供整个URL:

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

项目工作正常我只是想了解这个概念。

1 个答案:

答案 0 :(得分:0)

春天的全部意义在于你不必担心那些东西。 jsp在application.propperties中找到了属性的原因: 像:

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

对于形式和方法......它不像魔法般的发生。 您必须通过名称和方法进行校准的表格将更准确地映射到网址或操作

像:

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

要从控制器调用表单的值,首先必须将它们绑定到实体模型,并将表单的字段作为变量。 该方法如下所示:

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