我正在研究基于SpringMVC的网络应用程序。所以我在springmvc中创建了一个项目,并选择了eclipseIDE。Apache tomcat 8服务器和jre1.8,spring包的版本是4.2.5。在我的项目中,我创建了登录,登录后运行良好。我将页面重定向到另一个名为“branchinsert”的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;
}
显示url/ProjectName/modules/branch/branchsubmitinsert.jsp的404错误
实际上我期望的网址是/branch/branchinsert.jsp(这是通常会发生的事情),但这里创建了/branch/branchsubmitinsert.jsp url。为什么????
这是我的代码
网络.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;
}
}
因为您在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>
这意味着return < code >“branch/branch insert”将在< code >/modules/branch/branch insert . jsp 中找到JSP。
如果只使用字符串返回,则意味着它只是从jsp发送请求集的路径。因此,<code>viewResolver</code>不会添加前缀文件夹。
但是,如果您使用ModelAndView
,则viewResolver将添加前缀和后缀。
我将方法submit的返回类型更改为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?我不想单独问这个问题,这就是为什么我用我的答案来问这个问题。有人能帮我吗?
问题内容: 这是一个非常基本的问题。但是我无法在Java文档中找到答案,也无法对其进行测试,因为我不知道这种方法是否存在。 我可能会收到一个URL字符串,可能是 要么 然后我会得到可能以开头的资源路径,或者就像 我正在看课,可以处理第一部分,即获取hostURL使其成为HTTPS或HTTP请求。问题是附加资源路径。我必须手动检查它的第一个字母是否存在。我想知道此功能是否已经在某个类中。 问题答案:
问题内容: 我想创建一个字符串映射到实例。这是正确的方法吗? 问题答案: 每当我想使用A时,我都发现切片是正确的选择,例如
web.xml /we b-INF/spring/WEB MVC-config . XML 控制器 并存在索引.jsp文件 我想这是非常好的工作 但是! 为什么????为什么????为什么????为什么???? 更奇怪的是 ?????????????????????????????????????????????????? 控制器工作!!但不显示浏览器 这是怎么回事? 请帮助我。 和日志 名为“d
问题内容: 我有一个对HttpServletRequest对象做一些工作的服务,特别是使用request.getParameterMap和request.getParameter构造一个对象。 我想知道是否有一种简单的方法来获取以字符串形式提供的URL,例如 并轻松地将其转换为HttpServletRequest对象,以便可以使用单元测试进行测试?或者至少只是为了使request.getParam
我有一个给定格式的JSON数据: 我想把它转换成地图 我在Kotlin方面没有太多经验,我主要用python处理过此类案例,我成功地编写了以下内容: 我无法使用向地图键值添加数据,任何想法或线索都会有帮助,谢谢。
解决方案:将包重命名为,必须是主包的子包(包含main方法的子包) 我已经讨论过以下主题,但似乎没有一个对我有效: RequestMapping导致404错误,添加了regex(到Rest控制器,到方法),但仍然不会映射它。 Spring@RequestMapping,404 error在我的pom.xml中添加了,尽管我找不到更改它的路径(我怀疑可能存在问题,但我不确定)。 请求映射返回错误40