1

アプリケーションでいくつかの問題が発生しています。コントローラーから別のページに投稿される登録フォームがあります。このページには、登録フォームからのクエリの結果が表示されます。結果ページでレコードを選択すると、データが登録ページに返されます。ユーザーは、返されたレコードを更新するか、クエリを再度実行できる必要があります。

私が抱えている問題は、ユーザーが登録フォームにいて、結果ページに投稿されたクエリを実行すると、結果ページが表示されますが、URL は変更されません。登録 URL はhttp://localhost:8084/crimeTrack/citizen_registration.htm 、クエリ ボタンをクリックして結果ページに投稿されたときですhttp://localhost:8084/crimeTrack/citizen_registration.htm 。結果ページ (複数のレコードがある) でレコードがクリック/選択されたときの URL のままです。ユーザーは、選択されたレコードと共に登録ページに戻されます。ユーザーが更新またはクエリを再度実行するように表示されます。URL はhttp://localhost:8084/crimeTrack/getCitizen/1985121244.htm であり、ユーザーは登録ページにいます。

[クエリ/更新] をもう一度クリックすると、HTTP 400 エラーが発生し、URL が読み取らhttp://localhost:8084/crimeTrack/getCitizen/citizen_registration.htm/ れますが、これはコントローラーで有効な URL マッピングではありません。http://localhost:8084/crimeTrack/citizen_registration.htm登録ページがリクエストされたときのURLであるべきだと思います 。結果ページからの POST によってユーザーが登録ページに戻るタイミングがわかりません。URL は http://localhost:8084/crimeTrack/getCitizen/1985121244.htm 、添付の番号が市民番号である必要があります。以下は私のコードです。これらの呼び出しを正しく行っているかどうかわかりません。得られた結果の説明と、発生した問題の解決策が必要です。

ページはjqueryを使用して送信されます。

これは登録ページの例であり、他のすべてのページは同じパターンに従います

JScript

function submitPage(){   

    document.getElementById("citizenRegistration").action="citizen_registration.htm";
    //document.getElementById("citizenRegistration").target="_self";    
    document.getElementById("citizenRegistration").method = "POST";
    document.getElementById("citizenRegistration").submit();

}

シチズン_登録.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html lang="en">

    <head>  


    <title>Citizen Registration</title>

</head> 
    <body>              
            <div id="tab1" class="divGroup">
                <form:form id="citizenRegistration" name ="citizenRegistration" commandName="citizens">
                    ........................
                        <div class="buttons">   
                            <ol>
                                <li><input class="button" id="save" type="submit" name= "user_request" value="Save"/>
                                    <input class="button" id="update" type="submit" name= "user_request" value="Update"/>
                                    <input class="button" id="query" type="submit" name= "user_request" value="Query"/>
                                </li>       

                </form:form>
            </div>          
    </body>
</html>

市民リスト.jsp

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

<head>

   <script type="text/javascript">

   function submitPage(socialSecurityNumber){    

        document.getElementById("citizenList").action="getCitizen/1985121244.htm";//harded coded for testing
        //document.getElementById("citizenList").target="_self";    
        document.getElementById("citizenList").method = "POST";
        document.getElementById("citizenList").submit();

    }

 function GetCitizenTypeDescription(citizenTypeId){                 
        $.ajax({
        type:'GET',
        url:'getCitizenTypeDescription.htm',
        data:{citizenTypeId:citizenTypeId},
        dataType: 'text',       

        success: function (data) {      
        $('.citizenTypeId').each(function(i){               
                if($(this).val() === citizenTypeId){
                    //finds parent div
                    var parent = $(this).parent();
                    //search for child element wit class name citizenTypeDesc
                    var thisCitizenTypeDesc = parent.children('.citizenTypeDesc');                  
                    thisCitizenTypeDesc.text(data);
                }  
        });
    }


    });

}    
    <title>Citizen Search Results</title>

</head>
<body>
<form:form id="citizenList" name ="citizenList">
<div id ="content">
<c:forEach items="${citizens}" var="citizen">
<div id="table">    
    <div>
        <p><canvas class="canvas" height="240" width="320"></canvas>
    </div>
        <label class="citizenTypeDesc"></label></br>

        <a class="socialSecurityNumber" href="${citizen.socialSecurityNumber}">${citizen.fName}  ${citizen.lName}</a> 
        <input type="hidden" id="photo" value="${citizen.photo}" class="photos"/>
        <input type="hidden" id="socialSecurityNumber" value="${citizen.socialSecurityNumber}" />
        <input type="hidden" class="citizenTypeId" value="${citizen.citizenTypeId}"/>

</div>
</c:forEach>
</div>
</form:form>
</body>
</html>

CitizenRegistrationController.java

@Controller
public class CitizenRegistrationController {


    private final Logger logger = Logger.getLogger(getClass()); 

    @Autowired
    private CitizenTypeManager citizenTypeManager;
    ............

    Map<String, Object> myCitizenType    = new HashMap<String, Object>();
    .......

    @InitBinder("citizens") 
    protected void initBinder(WebDataBinder binder){        

        //removes white spaces 
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));

        //formats date 
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        //By passing true this will convert empty strings to null
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        dateFormat.setLenient(false);

      //binder.setValidator(new OfficerRegistrationValidation());
      binder.setValidator(citizenRegistrationValidation);

      binder.registerCustomEditor(Integer.class,new CustomIntEditor());


    }

    @RequestMapping(value="citizen_registration.htm", method = RequestMethod.GET)
    public ModelAndView loadPage(@ModelAttribute Citizens citizen, 
                                 BindingResult result,
                                 ModelMap m,
                                 Model model,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {



        try{
             logger.debug("In Http method for CitizenRegistrationController");       

             myCitizenType.put("citizenTypeList",       this.citizenTypeManager.getCitizenType());
             myGender.put("genderList",                 this.genderManager.getGenderList());             
             ......



            return new ModelAndView("citizen_registration");

        }catch(Exception e){

            logger.error("Exception in CitizenRegistrationController - ModelAndView loadPage "+e);
            request.setAttribute("error",e.getMessage());
            return new ModelAndView("error_page");  

        }   

    }

    @RequestMapping(value="citizen_registration.htm", method = RequestMethod.POST)
    public ModelAndView handleRequest(@Valid @ModelAttribute Citizens citizen, 
                                        BindingResult result,
                                        ModelMap m,
                                        Model model,
                                        @RequestParam(value="user_request") String user_request) throws Exception {


        try{
             logger.debug("In Http method for CitizenRegistrationController - Punishment Registration");
             logger.debug("User Request Is " + user_request);


                 if(result.hasErrors()){

                     logger.debug("Has Errors");
                    return new ModelAndView("citizen_registration");
                 }else{

                     //check if its a save of an update

                     if(user_request.equals("Save")){

                         citizenManager.RegisterCitizen(citizen);   
                         model.addAttribute("icon","ui-icon ui-icon-circle-check");
                         model.addAttribute("results","Record Was Saved");
                         return new ModelAndView("citizen_registration");

                     }else if (user_request.equals("Query")){
                         logger.debug("about to preform query");
                         //citizenManager.getListOfCitizens(citizen);
                         if(citizenManager.getListOfCitizens(citizen).isEmpty()){

                             model.addAttribute("icon","ui-icon ui-icon-circle-close");
                             model.addAttribute("results","Notice: Query Caused No Records To Be Retrived!");                            


                         }else{
                             model.addAttribute("citizens",citizenManager.getListOfCitizens(citizen));
                             return new ModelAndView("citizenList"); 


                         }                      

                     }else if (user_request.equals("Update")){
                         logger.info("About to do update");

                         citizenManager.UpdateCitizen(citizen);

                         return new ModelAndView("citizen_registration");                        
                     }                  
                 }

                     logger.debug("Has No Errors");     

            return new ModelAndView("citizen_registration");

        }catch(Exception e){

            logger.error("Exception in CitizenRegistrationController - ModelAndView loadPage "+e);
            //request.setAttribute("error",e.getMessage());

             return new ModelAndView("citizen_registration");

        }

    }

         @RequestMapping(value="getCitizen/{socialSecurityNumber}.htm", method = RequestMethod.POST)
         public ModelAndView getCitizen(@PathVariable Integer socialSecurityNumber,@ModelAttribute Citizens citizen, 
                                        BindingResult result,ModelMap m,Model model,HttpServletRequest request,  
                                        HttpServletResponse response) {

             try {
                 model.addAttribute("citizens",citizenManager.getCitizen(socialSecurityNumber));
                 //model.addAttribute("citizens",citizenManager.getCitizen(socialSecurityNumber));
            } catch (Exception e) {

                logger.error("Exception in CitizenRegistrationController - ModelAndView getCitizen "+e);
            }

            return new ModelAndView("citizen_registration");     

         }


     @RequestMapping(value="getCitizenTypeDescription.htm", method=RequestMethod.GET)
     public @ResponseBody String citizenTypeDescription(@RequestParam Integer citizenTypeId)throws Exception{

        String data = "No Data Found";

         try{

            data = citizenTypeManager.getCitizenTypeDescription(citizenTypeId);

         }catch(Exception e){
             data = e.getMessage();          
             logger.error("Exception In getCitizenTypeDescription.htm error : " + e);
         }

         return data;    

     }   
//setter methods    
    /**
     * @param citizenTypeManager the citizenTypeManager to set
     */
    public void setCitizenTypeManager(CitizenTypeManager citizenTypeManager) {
        this.citizenTypeManager = citizenTypeManager;
    }
    ................................

}

編集

return new ModelAndView("redirect:/citizenList.htm");ユーザーがクエリをクリックしたときにコントローラーで使用しようとし ましたが、取得しています404 Not Found - http://localhost:8084/crimeTrack/citizenList.htm"

サーブレット.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                           http://www.springframework.org/schema/beans/spring-context-3.0.xsd">




<!-- __________________________________________________________________________________________________ -->    

     <!-- Supports annotations and allows the use of @Controller, @Required, @RequestMapping -->
    <context:annotation-config/>    

    <context:component-scan base-package="com.crimetrack.business"/>
    <context:component-scan base-package="com.crimetrack.jdbc"/>
    <context:component-scan base-package="com.crimetrack.service"/>
    <context:component-scan base-package="com.crimetrack.web" />

    <mvc:annotation-driven />  

    <mvc:resources mapping="/resources/**" location="/public-resources/"/>

 <!-- __________________________________________________________________________________________________ -->    

    <!-- Forwards requests to the "/" resource to the "login" view -->  
    <mvc:view-controller path="/login" view-name="login"/>

    <!-- Forwards requests to the "/" resource to the "officer_registration" view -->  
    <mvc:view-controller path="/officer_registration" view-name="officer_registration"/>


    <!-- Forwards requests to the "/" resource to the "citizenList" view -->  
    <mvc:view-controller path="/citizenList" view-name="citizenList"/>


    <!-- Forwards requests to the "/" resource to the "citizen_registration" view --> 
    <mvc:view-controller path="/citizen_registration" view-name="citizen_registration"/>

<!-- __________________________________________________________________________________________________ -->    

    <!--  <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> --> 

    <!--  Is used to process method level annotations -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>    
<!-- __________________________________________________________________________________________________ -->    

    <!-- <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>  --> 

     <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basename" value="messages"/>
     </bean>


     <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<!-- __________________________________________________________________________________________________ --> 


      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>        
      </bean>



</beans>
4

2 に答える 2

2

問題は、あなたが言うときにここで相対パスを使用していることですaction="citizen_registration.htm"

に変更します

document.getElementById("citizenRegistration").action="/crimeTrack/citizen_registration.htm";

また

document.getElementById("citizenRegistration").action="../citizen_registration.htm";
于 2013-03-17T03:56:47.983 に答える
1

1) 送信の代わりに入力タイプをボタンに変更し、呼び出すアクションを渡して submitPage 関数を呼び出す onclick イベントを追加します。

<input class="button" id="save" type="button" name="user_request" value="Save"     onclick="submitPage('citizen_registration.htm');"/>
<input class="button" id="update" type="button" name="user_request" value="Update" onclick="submitPage('citizen_registration.htm');"/>
<input class="button" id="query" type="button" name="user_request" value="Query" onclick="submitPage('citizen_list.htm');"/>

2) submitPage 関数を変更して、クリックされたボタンに基づいてアクションを設定します

function submitPage(action){   
    document.getElementById("citizenRegistration").action = action;
    document.getElementById("citizenRegistration").method = "POST";
    document.getElementById("citizenRegistration").submit();
}

3) 2 つのコントローラー メソッドがあり、1 つは保存/更新にマップされ、もう 1 つはリストにマップされます。

@RequestMapping(value="citizen_list.htm", method = RequestMethod.POST)
public ModelAndView getCitizenList(@Valid @ModelAttribute Citizens citizen, BindingResult result, ModelMap m, Model model, @RequestParam(value="user_request") String user_request) throws Exception {

   try{
        logger.debug("about to preform query");
        //citizenManager.getListOfCitizens(citizen);
        if(citizenManager.getListOfCitizens(citizen).isEmpty()){
             model.addAttribute("icon","ui-icon ui-icon-circle-close");
             model.addAttribute("results","Notice: Query Caused No Records To Be Retrived!");                            
        }else{
             model.addAttribute("citizens",citizenManager.getListOfCitizens(citizen));
             return new ModelAndView("citizenList"); 
        } 
    }catch(Exception e){
        logger.error("Exception in CitizenRegistrationController - ModelAndView loadPage "+e);
        //request.setAttribute("error",e.getMessage());
         return new ModelAndView("citizen_registration");
    }
}

@RequestMapping(value="citizen_registration.htm", method = RequestMethod.POST)
public ModelAndView handleRequest(@Valid @ModelAttribute Citizens citizen,  BindingResult result, ModelMap m, Model model, @RequestParam(value="user_request") String user_request) throws Exception {

    try{
         logger.debug("In Http method for CitizenRegistrationController - Punishment Registration");
         logger.debug("User Request Is " + user_request);

         if(result.hasErrors()){
            logger.debug("Has Errors");
            return new ModelAndView("citizen_registration");
         }else{
             //check if its a save of an update
             if(user_request.equals("Save")){
                 citizenManager.RegisterCitizen(citizen);   
                 model.addAttribute("icon","ui-icon ui-icon-circle-check");
                 model.addAttribute("results","Record Was Saved");
                 return new ModelAndView("citizen_registration");
             } else if (user_request.equals("Update")){
                 logger.info("About to do update");
                 citizenManager.UpdateCitizen(citizen);
                 return new ModelAndView("citizen_registration");                        
             }                  
         }

        logger.debug("Has No Errors");     
        return new ModelAndView("citizen_registration");

    }catch(Exception e){
        logger.error("Exception in CitizenRegistrationController - ModelAndView loadPage "+e);
        //request.setAttribute("error",e.getMessage());
         return new ModelAndView("citizen_registration");
    }
}
于 2013-03-19T17:19:44.740 に答える