Spring MVC 控制器工作但未创建指定的响应 URL,它正在从请求映射字符串创建 url

2024-01-07

我正在研究基于 Spring MVC 的 Web 应用程序。所以我在spring mvc中创建了一个项目,我选择了eclipse IDE。Apache tomcat 8服务器和jre1.8,spring包的版本是4.2.5。在我的项目中,我创建了登录名,登录后工作正常,我将页面重定向到另一个名为“branchinsert.jsp”的jsp,该jsp放置在另一个文件夹中(login.jsp和branchinsert.jsp来自不同的文件夹)。在 Branchinsert.jsp Spring MVC 控制器正在工作,但没有创建指定的响应 URL,它正在从请求映射字符串创建 url,这意味着如果我给出如下所示的模式,

@RequestMapping(value="/branchsubmitinsert.travel", method=RequestMethod.POST)
public ModelAndView submitBranchInsert(@ModelAttribute BranchModel branchModel){

    ModelAndView modelAndView = new ModelAndView("/branch/branchinsert");

    return modelAndView;

}

显示 url /ProjectName/modules/branch/branchsubmitinsert.jsp 的 404 错误

实际上,我期望的网址是 /branch/branchinsert.jsp (这是通常发生的情况),但这里创建了 /branch/branchsubmitinsert.jsp 网址。为什么????

这是我的代码

web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns="http://java.sun.com/xml/ns/javaee" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" 
 version="3.0">
<display-name>CalicutTravels</display-name>
   <servlet>
     <servlet-name>spring-dispatcher</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
  </servlet>
<servlet-mapping>
  <servlet-name>spring-dispatcher</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping>
<listener><listener-class>
    org.springframework.web.context.ContextLoaderListener
</listener-class>

应用程序上下文.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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

      <context:component-scan base-package="com.classes.controller">
          <context:exclude-filter type="annotation"  
                expression="org.springframework.stereotype.Controller"/>
      </context:component-scan>

  </beans>

spring-dispatcher-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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  <context:annotation-config />
  <context:component-scan base-package="com.classes.controller">
  </context:component-scan>
  <mvc:annotation-driven/>

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

   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/modules/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <!-- declare beans -->

      <bean id="userDaoImplementation" 
       class="com.classes.dao.login.UserDaoImplementation" />
      <bean id="userServiceImplementation"

      class="com.classes.service.login.UserServiceImplementation" />

   <!-- declare datasource bean "jdbc:postgresql://hostname:port
   /dbname","username", "password");-->
  <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name="driverClassName" value="org.postgresql.Driver" />
   <property name="url" value="jdbc:postgresql://localhost:5432
   /db_enterpricer_travel" />
   <property name="username" value="vishnu" />
   <property name="password" value="root" />
  </bean>

  </beans>

Branchinsert.jsp 的表单字段

 <form action='branchsubmitinsert.travel' id='brach_submit' method='post' >
            <fieldset class="well the-fieldset">
                <legend class="the-legend">  INSERT </legend>

                    <table>
                        <tr>
                            <th> Branch Name :</th>
                            <td> <input type="text" name='branchName' id='branchName' /></td>
                        </tr>
                        <tr>
                          <td colspan="2" align="right"><input type="button" class="btn btn-default" value='INSERT' onclick='return validateBranchInsert()'/></td>
                        </tr>
                    </table>


             </fieldset>
        </form>

控制器类BranchController.java

 package com.classes.controller.branch;

 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.ModelAttribute;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.portlet.ModelAndView;

 import com.classes.service.branch.BranchModel;


 /**
 * @author vishnu
 *
 */

 @Controller
 @RequestMapping("/branch")
 public class BranchController {

 @RequestMapping(value="/branchinsert.travel", method=RequestMethod.GET)
 public ModelAndView getBranchInsert(){

    ModelAndView modelAndView = new ModelAndView("/branch/branchinsert");
    return modelAndView;

 }

 @RequestMapping(value="/branchupdate.travel", method=RequestMethod.GET)
 public ModelAndView getBranchUpdate(){

    ModelAndView modelAndView = new ModelAndView("/branch/branchupdate");
    return modelAndView;

 }

 @RequestMapping(value="/branchshow.travel", method=RequestMethod.GET)
 public ModelAndView getBranchShow(){

    ModelAndView modelAndView = new ModelAndView("/branch/branchshow");
    return modelAndView;

 }

 @RequestMapping(value="/branchdelete.travel", method=RequestMethod.GET)
 public ModelAndView getBranchDelete(){

    ModelAndView modelAndView = new ModelAndView("/branch/branchdelete");
    return modelAndView;

 }

@RequestMapping(value="/branchsubmitinsert.travel",
method=RequestMethod.POST)
public ModelAndView submitBranchInsert(@ModelAttribute BranchModel 
branchModel){

    ModelAndView modelAndView = new ModelAndView("/branch/branchinsert");
    modelAndView.addObject("branch",branchModel);
    return modelAndView;

}

}

我将方法submitBranchInsert的返回类型更改为String,如下所示

 @RequestMapping(value="/branchsubmitinsert.travel", method=RequestMethod.POST)
public String submitBranchInsert(ModelMap model,@ModelAttribute BranchModel branchModel){

    //ModelAndView modelAndView = new ModelAndView("/branch/branchinsert");
    //modelAndView.addObject("branch",branchModel);
    //return modelAndView;
    model.addAttribute("branch", branchModel);
    return "/branch/branchinsert";

}

现在可以了,但为什么我不能在这里使用 ModelAndView 而不是 String 中的 Model , 对于 Spring MVC 的专家来说,我可能听起来很愚蠢。 我只是想知道什么时候应该使用 ModelAndView 而不是 String 中的 Model ? 我不想单独问这个问题,这就是为什么我用我的答案本身来问这个问题。 有人可以帮助我吗?

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

Spring MVC 控制器工作但未创建指定的响应 URL,它正在从请求映射字符串创建 url 的相关文章

随机推荐

  • 在 boost 中等待多个条件变量?

    我正在寻找一种等待多个条件变量的方法 IE 就像是 boost condition variable cond1 boost condition variable cond2 void wait for data to process bo
  • 将 Web 请求绑定到特定网络适配器

    背景 我有一个带有 2 个网络适配器的移动嵌入式设备 Ubuntu Mono 一种是WiFi 另一种是GSM 要求是当WiFi适配器连接到互联网时 在预定AP的范围内 应用程序上传数据 HTTPS 发布 通过 WiFi 但当 WiFi 不可
  • mod_rewrite - 排除 url

    我需要一个 mod rewrite 来重定向所有http要求https 但我想排除一些网址 force https RewriteCond HTTPS off RewriteCond HTTP HOST secure NC RewriteC
  • 在捆绑扩展中获取 Symfony2 环境

    在我的 Symfony2 捆绑扩展中services yml正在加载中 loader new Loader YamlFileLoader container new FileLocator DIR Resources config load
  • 如何在Android中设置菜单标题[重复]

    这个问题在这里已经有答案了 我正在尝试动态设置菜单的标题 检索并设置它 ItemView menuTitle ItemView findViewById R id menu filter menuTitle setTitle TITLE H
  • 更改 Visual Studio Code 的选项卡栏颜色

    有没有办法在 VS Code 中配置选项卡栏的背景颜色 我知道有一个workBench colorCustomization允许使用 VS Code 更改主题颜色的设置settings json文件 是否可以使用该设置更改它 Tab bar
  • 数据与 [UInt8]

    Swift 两者都提供Data and UInt8 类型 它们做的事情非常相似 两者有何区别 设计新的 API 时 首选类型是什么 UInt8 本质上是一个字节数组 一个字节 我相信你知道 由 8 位组成 虽然 NSData 不仅仅是一个字
  • 如何清理 Arel SQL?

    我有以下 Arel SQL Arel sql users last donated at IS NOT NULL AND users last donated at lt User ACTIVE DONOR WITHIN DAYS days
  • 使用 Jquery fadeout 进行页面过渡

    您好 提前感谢您提供任何解决方案 我一直在尝试在用户切换页面时添加淡入和淡出功能 我已经尝试了在这里和其他论坛上找到的许多解决方案 但似乎都不适用于淡出 淡入效果很好 我只需将 ghostly 添加到 body 标签即可 我所做的一切都对淡
  • Python从文本文件中删除标点符号

    我正在尝试从文本文件中删除标点符号列表 但只有一个与连字符分隔的单词问题 例如 如果我有 post trauma 这个词 我会得到 posttrama 相反 我想得到 post trauma 我的代码是 punct with open my
  • 创建 csv 文件,其中每一行是一个列表中的一些嵌套列表

    我有体育比赛的名单 table Volleyball Europe European Championships Today 17 00 Moldova Cyprus 2 00 1 72 Handball Slovenia 1 NLB Li
  • 通过远程 Active Directory 单点登录 Django 站点

    我使用 Django 为客户开发了一个内联网 用户通过 Active Directory 登录到他们的计算机 目前 我通过标准 Django contrib auth 登录它们 并通过自定义登录后端使用 Active Directory 我
  • 如何迭代字典列表

    我的代码是 index 0 for key in dataList index print dataList index key 似乎可以很好地打印字典键的值index 0 但是 我不知道如何迭代未知数量的字典dataList 您可以只迭代
  • fcm 订阅主题

    我正在尝试使用 FCM 向所有主题订阅者发送通知 首先 我需要让用户订阅一个主题 我做什么 我使用 JavaScript Firebase Cloud Messaging 进行网络推送通知 1 获取FCM实例 var messaging f
  • 将 label_suffix 添加到 ModelForm

    我该如何更改label suffix on a ModelForm 我希望模型表单从所有标签中删除 当表单被实例化时 你可以通过label suffix如下 list form ListForm label suffix
  • 可以更改不同文件的 SASS/Compass 输出文件夹吗?

    我想知道Compass是否可以将文件输出到不同的目录 我有一个相当大的项目 虽然大多数 CSS 文件都放在 css 文件夹中 但其他文件需要放在 admin css 文件夹中 我现在正在对它们进行符号链接 但如果我可以定义一个可以在其他地方
  • 如何获取 BGL 图中的 OutEdgeList

    我使用由 boost 图库制作的图 并希望访问 OutEdgeList 来获取 设置一些内部信息 用于构建 OutEdgeList 的向量的保留大小 我广泛搜索了文档 但没有找到返回指向 OutEdgeList 的引用或指针的函数 成员 我
  • Android 文件(Java 和 XML)被奇怪的 XML 取代 [重复]

    这个问题在这里已经有答案了 我在使用 Android Studio v2 3 3 构建于 2017 06 06 时遇到了一个奇怪的问题 我的一个 Java 文件被替换为似乎由 Android Studio 生成的不相关的 XML 我不认为我
  • 如何从数据库获取日期到html日期选择器

    如何从数据库获取日期值到html datepicker 以下是我的html代码
  • Spring MVC 控制器工作但未创建指定的响应 URL,它正在从请求映射字符串创建 url

    我正在研究基于 Spring MVC 的 Web 应用程序 所以我在spring mvc中创建了一个项目 我选择了eclipse IDE Apache tomcat 8服务器和jre1 8 spring包的版本是4 2 5 在我的项目中 我