Spring MVC Controller正在工作但未创建指定的响应URL,它是从请求映射字符串创建URL
问题内容:
我正在研究基于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 Controller正在运行,但未创建指定的响应URL,它是从请求映射字符串创建url,这意味着如果我给出的模式类似于下面所述,
@RequestMapping(value="/branchsubmitinsert.travel", method=RequestMethod.POST)
public ModelAndView submitBranchInsert(@ModelAttribute BranchModel branchModel){
ModelAndView modelAndView = new ModelAndView("/branch/branchinsert");
return modelAndView;
}
显示网址404错误/ProjectName/modules/branch/branchsubmitinsert.jsp
实际上,我期望的网址是/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>
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而不是Model?我不想单独问这个问题,这就是为什么我要用我的答案本身来问这个问题。有人可以帮助我吗?