当我的 Web 项目中的 .jsp 页面被直接点击时,尝试让 Spring MVC Controller 触发。
调度程序 servlet 的 Web XML:
<servlet>
<servlet-name>MYAPP</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MYAPP</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
在调度程序配置中,我定义了以下 bean:
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Controller 类:
package com.mydomain.controllers;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.mydomain.security.Authorization;
@Controller
public class CustomerController {
@RequestMapping("/customer")
public String customer(HttpServletRequest request) {
if (Authorization.SessionCheck(request))
return "../customer/customer";
else
return "redirect:http://elsewhere.com";
}
}
从根本上讲,我在/WebContent/customer/customer.jsp 中有一个 jsp 页面 如果在浏览器中我点击 http://myhost.com/MYAPP/customer , Controller 处理上面的客户方法就好了。但是,如果我点击 http://myhost.com/MYAPP/customer/customer.jsp ,那么 Controller 就永远不会触发,并且只呈现 customer.jsp 页面。当 .jsp 页面被点击时,如何让 Controller 进行处理?
最佳答案
However, if I hit http://myhost.com/MYAPP/customer/customer.jsp, then the controller never fires
为什么你会这么期待呢?您的 @Controller
的处理程序方法已映射到
@RequestMapping("/customer")
这就是它将处理的路径。如果您希望它处理其他路径,请添加另一个路径或更多处理程序方法。
至于为什么呈现 JSP,那是因为它位于 Web 应用程序的 WebContent
文件夹中。那里的任何文件都可以直接访问。
关于java - Controller 未在 .jsp 页面上触发 MVC Spring JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25248703/