Spring MVC Controller正在工作但没有创建指定的响应URL,它正在从请求映射字符串创建url

时间:2016-05-24 10:25:32

标签: spring jsp spring-mvc

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

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

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

    return modelAndView;

}

显示网址​​/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>

的applicationContext.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>

弹簧调度-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;

}

}

2 个答案:

答案 0 :(得分:0)

由于您在viewResolver的前缀中提到了modules文件夹,因此它就像那样工作..

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

这意味着返回"branch/branchInsert"会在/modules/branch/branchInsert.jsp中找到jsp。

如果你只使用字符串返回意味着它只是来自你发送requset的jsp的路径。那么 viewResolver 不会添加前缀文件夹。

但是如果您使用ModelAndView,viewResolver将添加前缀和后缀。

答案 1 :(得分:0)

我将方法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? 我不想单独问这个问题,这就是为什么我用我的答案本身问这个问题。 有人可以帮帮我吗?