2017-09-13 73 views
0

我在春天的web項目的工作,我有很多的JSP文件以及許多控制器,但我不能夠抓住如何Spring如何將jsp的url映射到各自的控制器?

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

形式自動映射到相應的控制器,其中updateCustomer被定義爲。還有其他控制器,但網址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

這是怎麼發生的?而現在,當我需要一個按鈕,一些其他控制器我需要給整個網址如下:

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

該項目工作的所有罰款,我只是想了解這個概念。

+0

可以使用''或'<彈簧:url值= 「/東西」 VAR = 「URL」 htmlEscape =」真正的「/>'在鏈接而不是它們是'jsp,Spring'標籤。請閱讀更多關於它們以讓自己舒服 – emotionlessbananas

+0

該項目已經正常工作,我正在尋找的概念不是一種選擇。 – Jstorm99

回答

0

春天的全部是你不必擔心那些東西。 JSP的又找到在application.propperties的propperty的原因: 像:

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

對於形式和方法......它不喜歡它magicly發生。 的表格,你必須按名稱CAL和方法eighter映射到一個URL或行動

,如:

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

要呼叫控制器形式的值,你首先必須將它們綁定到一個實體模型以表單的字段作爲變量。 的方法將如下所示:

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

正如我已經顯示的控制器文件一樣,唯一的請求映射是/買方,但是我們沒有在form的action屬性中提到過,所以它是如何被重定向到它自己的BuyerController的。 – Jstorm99

+0

你不會顯示被加載的javascript文件,其中一個被稱爲formvalidator,所以我覺得你的表單是javascript的beïnghandeld。控制器類中沒有任何東西在處理它。我將發佈一個新的答案,以顯示控制器如何調用窗體... –

+0

我的不好。它映射到您的控制器。您的formaction是updateCustomer,因此這是您點擊提交按鈕時形成的網址。你的第二個方法被映射到那個。所以這就是所謂的 –

相關問題