我想重定向到另一个 jsp 页面。在 Spring MVC 中,但我收到 HTTP 状态 404 -/registration.jsp 错误。我想做的就是打开页面。注册新个人页面或从主页添加新产品页面
路径或项目层次结构: 我不想使用 JSTL 或标签。最好的方法是什么?
我在我的 JSP 页面上尝试过:
<ul class="nav child_menu" style="display: none"> <li><a href="${pageContext.request.contextPath}/grcon?path=register">User Registartion</a>
</li>
<li><a href="${pageContext.request.contextPath}/grcon?path=usermang">User Management</></li></ul>
还有我的 Controller :
@RequestMapping(value ="/grcon/{path}" ,method = RequestMethod.GET)
public ModelAndView getGrcon(@PathParam(value = "path") String path)
{
ModelAndView modegeron = null;
if (path != null && path.equals("register")) {
modegeron = new ModelAndView("registeruser");
} else if (path != null && path.equals("usermang"))
{
modegeron = new ModelAndView("manageuser");
} else {
modegeron = new ModelAndView("index");
}
return modegeron;
}
}
spring-dispatcher-servlet.xml
<context:component-scan base-package="recon.controller" />
<!-- viewResolver tell which viewResolver to use it tell the location of
the view in the project -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources location="/resoures/**" mapping="/resoures/"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
</beans>
web.xml
<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>
<session-config>
<session-timeout>15</session-timeout>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
HTTP 消息
HTTP Status 400 - Required String parameter 'path' is not present
type Status report
message Required String parameter 'path' is not present
description The request sent by the client was syntactically incorrect.
Apache Tomcat/7.0.67
最佳答案
您可以在 Controller 或 jsp 中选择请求参数或 url 模板变量。要使用 url 模板变量,请将 @PathParam(value="path") 替换为 @PathVariable(value = "path")。
在您的 jsp 中将链接 url 替换为 href="${pageContext.request.contextPath}/grcon/register 和 href="${pageContext.request.contextPath}/grcon/usermang
要使用请求参数,请将@PathParam(value = "path") 替换为@RequestParam(value = "path")。然后维护您的 jsp url 链接,因为它们已经使用了请求参数
注意。 PathParam 注释不是 Spring MVC 的一部分。它是与 JEE 标准一起使用的 JAX-RS 注释
关于java - Spring JSP 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35898479/