Spring MVC 页面未使用 simpleformcontroller 渲染成功页面

发布于 2024-12-09 19:05:31 字数 2627 浏览 0 评论 0原文

我编写了简单的 spring mvc 应用程序。但我无法将一个页面重定向到另一页面。 Controller下面提到了代码片段

我在Claims-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props><prop key="/loginpage.htm">loginFormController</prop></props>
    </property>
    </bean>

    <bean id="loginFormController" class="com.aims.controller.LoginFormController">
    <property name="sessionForm"><value>true</value></property>
    <property name="commandName"><value>LoginFormCommand</value></property>
    <property name="commandClass"><value>com.aims.commands.LoginFormCommand</value></property>
    <property name="validator"><ref bean="loginformValidator"/></property> 
    <property name="formView"><value>loginpage</value></property>
    <property name="successView"><value>body</value></property>

    </bean>
    <bean id="loginformValidator" class="com.aims.validator.LoginFormValidator"/>


    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property> 

        <property name="suffix"><value>.jsp</value></property>
    </bean>
</beans>

public class LoginFormController extends SimpleFormController {     
  public ModelAndView onSubmit(Object command, BindException bindException) throws Exception {
 System.out.println("LoginFormController:onSubmit============");
 LoginFormCommand loginform = (LoginFormCommand) command;
 System.out.println("username" + loginform.getUsername() + "Password"
            + loginform.getPassword()); 
 return   new ModelAndView(new RedirectView("/WEB-INF/view/jsp/"
            + getSuccessView()));
 }}

我有两个 jsp,一个是

Webroot>loginpage.jsp
view->jsp>body.jsp

当浏览器打开时自动调用 loginpage.jsp(web.xml>welcome-file) ,成功后我尝试调用 view->jsp> body.jsp。但它没有移动到 body.jsp。请需要帮助。

Iam wrote simple spring mvc apps.But I unable to redirect one page to another page. I mentioned code snippet below

Claims-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props><prop key="/loginpage.htm">loginFormController</prop></props>
    </property>
    </bean>

    <bean id="loginFormController" class="com.aims.controller.LoginFormController">
    <property name="sessionForm"><value>true</value></property>
    <property name="commandName"><value>LoginFormCommand</value></property>
    <property name="commandClass"><value>com.aims.commands.LoginFormCommand</value></property>
    <property name="validator"><ref bean="loginformValidator"/></property> 
    <property name="formView"><value>loginpage</value></property>
    <property name="successView"><value>body</value></property>

    </bean>
    <bean id="loginformValidator" class="com.aims.validator.LoginFormValidator"/>


    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property> 

        <property name="suffix"><value>.jsp</value></property>
    </bean>
</beans>

Controller:

public class LoginFormController extends SimpleFormController {     
  public ModelAndView onSubmit(Object command, BindException bindException) throws Exception {
 System.out.println("LoginFormController:onSubmit============");
 LoginFormCommand loginform = (LoginFormCommand) command;
 System.out.println("username" + loginform.getUsername() + "Password"
            + loginform.getPassword()); 
 return   new ModelAndView(new RedirectView("/WEB-INF/view/jsp/"
            + getSuccessView()));
 }}

I have two jsp one is

Webroot>loginpage.jsp
view->jsp>body.jsp

When the browser opens its automatically called loginpage.jsp(web.xml>welecome-file) and after success iam trying to call view->jsp>body.jsp.But it doesn't move to body.jsp.Please need help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神也荒唐 2024-12-16 19:05:31

对于重定向视图,您必须指定目标的实际 URL,而不是内部 jsp 的路径。 Spring MVC 不会渲染 jsp,而是将用户重定向到此 URL。

示例:new ModelAndView(new RedirectView("/example/helloworld.html"))

当然,目标必须存在。

With a redirect view, you must specify the actual URL of the target, not a path to an internal jsp. Instead of rendering a jsp, Spring MVC will redirect the user to this URL.

Example: new ModelAndView(new RedirectView("/example/helloworld.html")).

Of course, the target has to exist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文