在URL中键入JSP文件扩展名时未调用Spring Controller

时间:2012-02-26 16:49:44

标签: spring jsp url spring-mvc spring-security

我正在使用Spring MVC和Spring Security 3.

我的问题是,如果我像www.mydomain.com/ app.do 那样调用servlet,一切正常,意味着控制器被调用。但是,如果调用www.mydomain.com/ app.jsp ,则不会调用控制器。同样适用于 login.do servlet和 login.jsp 文件。

物理文件 app.jsp 位于根 / 目录中。

这是Spring MVC的预期行为吗?我可以以某种方式强制弹簧mvc也处理JSP文件吗?或者甚至禁止拨打网址?这通常如何处理?

的web.xml

...
    <!-- Spring Security filter mapping -->   
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> 

    <!-- Spring MVC servlet mapping -->
    <servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
...

弹簧MVC-servlet.xml中

...
    <context:component-scan base-package="ch.hobus.metronom.server" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>
...

弹簧安全XML

...
<http auto-config="true">
    <intercept-url pattern="/app*" access="ROLE_USER" />
    <intercept-url pattern="/metronom/**" access="ROLE_USER" />
    <intercept-url pattern="/gwt/**" access="ROLE_USER" />
    <intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
    <form-login login-page="/login.do" 
                    default-target-url="/app.do"
                    authentication-failure-url="/loginfailed.do" />
    <logout logout-success-url="/logout.do" />
</http>
...

LoginController.java

@Controller
public class LoginController {

  @RequestMapping(value = "/login.do", method = RequestMethod.GET)
  public String login(ModelMap model) {
    return "login";
  }

  @RequestMapping(value = "/loginfailed.do", method = RequestMethod.GET)
  public String loginerror(ModelMap model) {
    model.addAttribute("error", "true");
    return "login";
  }

  @RequestMapping(value = "/logout.do", method = RequestMethod.GET)
  public String logout(ModelMap model) {
    return "login";
  }

}

AppController.java

@Controller
public class AppController {

  @RequestMapping(value = "/app.do", method = RequestMethod.GET)
  public ModelAndView app(ModelMap model, Principal principal) {
    ModelAndView mav = new ModelAndView();
    mav.setViewName("app");
    mav.addObject("username", principal.getName());
    mav.addObject("message", "Metronom's benutzerdefiniertes Formular");
    return mav;
  }

}

1 个答案:

答案 0 :(得分:2)

是的,这是预期的行为。如果你要求JSP,你会得到一个JSP。解决方案:

  • 不包含任何指向Web应用程序中任何JSP的链接
  • 将JSP存储在/ WEB-INF
  • 将Spring servlet映射到/*(但是它也会针对图像,CSS文件等进行调用。您必须使用mvc:resources来处理这种情况。)