2017-09-13 17 views
0

私は多くのJSPファイルと多くのコントローラを持って、春のWebプロジェクトに取り組んでいますが、私はこのSpringがjspからそれぞれのコントローラにURLをマップする方法は?

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


} 

私は保存ボタンをクリックすると、今の質問は、形成されたURLがあるさ:
http://localhost:8080/82ism/buyer/updateCustomer

これはどうでしたか?私はいくつかの他のコントローラのボタンを必要とするとき、今、私は次のように全体のURLを与える必要があります。

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

プロジェクトは、私はちょうどこの概念を理解しようとしているすべての罰金を働いています。

+0

''や '<春:URL値= "/何か" VAR = "URL" htmlEscape =」 true "/>'の代わりに 'jsp、Spring'タグがあります。 – emotionlessbananas

+0

プロジェクトはすでにうまくいきました。私はコンセプトを探しています。 – Jstorm99

答えて

0

春の全体的なポイントはあなたがそのことを心配する必要はありません。それはmagicly起こるようではない...

フォームや方法については
spring.mvc.view.prefix=/WEB-INF/jsp/ 
spring.mvc.view.suffix=.jsp 

:よう : のJSPのはproppertyのapplication.propperties内の見つかった原因を取得します。 名前および方法によって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

コントローラファイルも表示しているので、唯一のリクエストマッピングは/ buyerですが、フォームのaction属性には言及していません。それで、どのようにBuyerControllerにリダイレクトされるのでしょうか。 – Jstorm99

+0

あなたは、ロードされているjavascriptファイルを表示しない、1つはformvalidatorと呼ばれているので、私はあなたのフォームがjavascriptによってbeeng handeldだと推測します。あなたのコントローラクラスの何もそれをハンドリングしていません。コントローラがフォームをどのように呼び出すかを示す新しい答えを投稿します。 –

+0

私の悪いです。あなたのコントローラにマップされています。あなたのformactionはupdateCustomerなので、submitボタンをクリックしたときに作成されたurlです。あなたの2番目の方法はそれにマップされます。同じコントローラーを使用している場合、これは –