0

嗨,我刚开始学习 Spring mvc,我使用 spring mvc @annotation,我有 3 个 servlet 调度程序(appservlet、admin、student):

web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->

    <servlet>
        <servlet-name>admin</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>   
    <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/admin-servlet.xml</param-value>
        </init-param>
    </servlet>

     <servlet>
        <servlet-name>student</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    </servlet>

    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>admin</servlet-name>
        <url-pattern>*.admin</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>student</servlet-name>
        <url-pattern>*.student</url-pattern>
    </servlet-mapping>
</web-app> 

在对方法进行测试后,我希望重定向到另一个 servlet 调度程序,无论是 .admin 还是 .student。请问我该怎么做?

@控制器

@Controller
public class AuthentificationController {

    @RequestMapping(value = "verifier", method = RequestMethod.POST)
    public ModelAndView redir(@ModelAttribute("per") Personne per,
            HttpSession session) {

        if (p1.equals(per)) {
            session.setAttribute("login", per);
            ModelAndView M = new ModelAndView(".admin");
            return M;
        }

        if (p2.equals(per)) {
            ModelAndView M = new ModelAndView(".student");
            return M;
        } else {
            ModelAndView M = new ModelAndView("home");
            return M;
        }

    }
}

问题ModelAndView返回(.jsp),但我想要另一个 servlet-dispatcher 的重定向!

4

1 回答 1

0

给你:

@RequestMapping(value = "verifier", method = RequestMethod.POST)
public String redir(@ModelAttribute("per") Personne per, HttpSession session) {
    if (p1.equals(per)) {
        session.setAttribute("login", per);
        return "redirect:.admin";
    } else if (p2.equals(per)) {
        return "redirect:.student"
    } else {
        return "home";
    }
}
于 2015-06-24T10:06:01.310 回答