Spring MVC 页面 HTTP 状态 400 和不正确的 URL

2023-12-06

我在使用该应用程序时遇到了一些问题。我有一个注册表单,该注册表单从控制器发布到另一个页面 该页面显示注册表中的查询结果。在结果页面上,我选择一条记录,它返回给我的是 数据到注册页面。用户应该能够在记录返回后更新记录或再次执行查询。

我遇到的问题是当用户在注册表单上执行查询时,它们会发布到结果页面 结果页面会显示,但 url 不会更改。注册网址是http://localhost:8084/crimeTrack/citizen_registration.htm当通过单击查询按钮发布到结果页面时,网址仍然是http://localhost:8084/crimeTrack/citizen_registration.htm当在结果页面(有多个记录)上单击/选择一条记录时,用户将返回到注册页面,并显示 选定的记录并显示给用户现在再次执行更新或查询,url 是http://localhost:8084/crimeTrack/getCitizen/1985121244.htm用户现在位于注册页面。

如果我再次单击查询/更新,我会得到一个HTTP 400 错误并且网址正在读取http://localhost:8084/crimeTrack/getCitizen/citizen_registration.htm/这不是控制器中有效的 URL 映射。我认为网址应该是http://localhost:8084/crimeTrack/citizen_registration.htm当注册页面为 要求。我不确定结果页面的 POST 何时将用户带回到注册页面,网址应该是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;
    }
    ................................

}

Edit

我尝试使用return new ModelAndView("redirect:/citizenList.htm");在控制器中,当用户单击查询时,我得到404 Not Found - http://localhost:8084/crimeTrack/citizenList.htm"

Servlet.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>

问题是当你说时你在这里使用相对路径action="citizen_registration.htm".

将其更改为

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

or

document.getElementById("citizenRegistration").action="../citizen_registration.htm";
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring MVC 页面 HTTP 状态 400 和不正确的 URL 的相关文章

随机推荐

  • 如何确保每次迭代后释放每个“子”进程的文件句柄?

    我采取了以下计划来自 Rust 文档std process Command 经过一些迭代后它停止工作 use std process Command use std process Stdio fn main loop let mut ec
  • Python:使用 xmltodict 获取值

    我有一个如下所示的 XML 文件
  • 禁用 Qt 3d 中的所有光源

    在我的公司 从旧的 3D 引擎转向 Qt3d 这项工作的目标之一是将旧 3D 引擎的渲染视图与 Qt3d 渲染进行比较 为此 我编写了一个小型示例应用程序 我可以在其中比较新旧渲染 仍然存在很多差异 我的第一个想法是切换两个引擎中的所有光源
  • 如何在 awk 中将分隔字符串拆分为数组?

    当字符串包含管道符号时如何拆分字符串 在里面 我想将它们分成数组 I tried echo 12 23 11 awk split 0 a print a 3 a 2 a 1 效果很好 如果我的字符串就像 12 23 11 那么如何将它们拆分
  • 使用母版页引用 Aspx 页面中的 CSS 表

    标题中已经很好地说明了问题 通常我会使用
  • 瞬态变量有什么用? [复制]

    这个问题在这里已经有答案了 可能的重复 为什么Java有瞬态变量 瞬态关键字将用于防止特定变量的序列化 但为什么我们不应该序列化数据呢 内心有安全感吗 有些类本质上是不可序列化的 因为它们代表管理 Java 环境之外的资源 例如一个File
  • 非 ACID 数据库合规性对现实世界有哪些影响?

    具体来说 是否存在数据丢失的风险 我正在考虑运行一个密集型事务处理系统 其中最重要的是不要丢失任何内容 是否有在银行交易处理等关键任务应用程序中使用 NoSQL 的示例 坦白说 缺乏 ACID 意味着您无法保证原子性 一致性 隔离性或持久性
  • 如何在 Excel 中运行网站表单而无需使用 Sendkeys?

    我正在尝试使用 Excel VBA 在网站上填写表格 我创建了一个 InternetExplorer Application 导航到该页面并查找了一堆数据 使用If UserForm1 TC2 Value True Then或者类似的东西P
  • 我可以在退出区域中获取信标数据吗?我如何检测新信标何时进入该区域?

    当在该区域检测到多个信标时 我怎样才能找到哪一个是最新检测到的 另外 在信标中 我如何知道特定信标已离开该区域 解决这个问题的典型方法是结合信标监控和信标测距 您可以使用信标测距来读取各个标识符 并保留之前见过的信标的 地图 如下所示 pr
  • 有没有办法在 Microsoft Azure 存储资源管理器或 Azure 门户中查看托管磁盘的 blob?

    当我使用 Azure 资源管理器创建 VM 时不受管理的磁盘 我可以在其中查看其 vhd微软Azure存储资源管理器和 或 Azure 门户中名为 vhds 的子容器中指定存储帐户的 Blob 容器 当我使用 Azure 资源管理器创建 V
  • Internet Explorer 10 - 如何应用灰度滤镜?

    此 CSS 代码在 Internet Explorer 9 之前运行得非常好 img gray filter url data image svg xml utf8
  • Spring Boot 3.0.0 中抛出异常的 HttpStatus 未正确处理

    我刚刚迁移到 SpringBoot 3 0 0 但发现应用程序在抛出异常时无法正确处理 HttpStatus 它总是给出 403 FORBIDDEN 或为未经身份验证的请求配置的其他代码 似乎在捕获异常或其他原因后身份验证就会丢失 我在这里
  • 重载不能仅因返回类型而不同

    我正在尝试重载一个函数 因此当在其他地方使用该函数时 它将正确显示结果 即项目数组 void 或单个项目 getSelected type void getSelected type IDataItem getSelected type I
  • 默认的切片索引*真的*是多少?

    来自Python文档docs python org tutorial introduction html strings 切片索引有有用的默认值 省略的第一个索引默认为零 省略的第二个索引默认为被切片的字符串的大小 对于标准情况 这很有意义
  • 如果哈希码被覆盖,使其仅返回常量,那么 Hashmap 键将如何表现?

    我有一个关于Java的小问题Hashmap 如果我覆盖hashCode方法使得 Override public int hashCode return 9 这将导致所有HashMap键具有相同的索引 它们是否会被放置在映射中的链表结构中 或
  • Canvas 中的 onClickListener

    我正在开发一个应用程序 该应用程序以图像作为索引来选择活动将开始的特定图像 但我不知道如何在 Canvas 中设置 onClickListener 或 onTouchListener 这是我的代码 public class DrawView
  • Oracle 中的 TO_NUMBER 函数出现奇怪的问题

    如果记录数超过特定数量 n 则在 varchar2 列的 where 子句中执行 to number 函数时 我遇到间歇性问题 我使用 n 是因为没有发生这种情况的确切记录数 在一个 DB 上 它发生在 n 为 100 万之后 而在另一个
  • 连接耳机时如何将默认音频路由到耳机?

    我正在开发一个应用程序 其中我们只需要使用耳机插孔作为按钮 要求 连接耳机时通过耳机播放默认音频 通话 不需要通过耳机播放音频 有很多通过扬声器和耳机以及蓝牙耳机路由音频的示例 但没有关于在连接耳机时通过设备的耳机路由音频的示例 我已经尝试
  • 没有 www 则不会出现网络字体

    我试图到处寻找答案 我有一个唱歌网站www hugeone co uk 当地址类似于此链接 带有 www 时 一切正常 但是 如果我只输入hugeone co uk 网络字体就不会出现 并且我收到消息 跨源请求被阻止 同源策略不允许读取 远
  • Spring MVC 页面 HTTP 状态 400 和不正确的 URL

    我在使用该应用程序时遇到了一些问题 我有一个注册表单 该注册表单从控制器发布到另一个页面 该页面显示注册表中的查询结果 在结果页面上 我选择一条记录 它返回给我的是 数据到注册页面 用户应该能够在记录返回后更新记录或再次执行查询 我遇到的问