【问题标题】:changing dispatcher xml file to change web page routing更改调度程序 xml 文件以更改网页路由
【发布时间】:2013-07-01 18:20:52
【问题描述】:

我正在使用 Spring MVC 和 GAE。我必须用一堆数据调用一个外部 URL。他们通过致电 www.mystuff.com/results.jsp 将结果返回给我。我需要通过控制器重新路由,而不是显示 jsp 视图。如何更改调度程序 servlet 来做到这一点?

我的 web.xml 和 mvc-dispatcherservlet 和控制器注释一样非常普通。

Web.xml

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

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
    </listener>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <filter>
        <filter-name>ObjectifyFilter</filter-name>
        <filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>ObjectifyFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

mvc 调度程序

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schem...-beans-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schem...ontext-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.hmt1.controller" />
    <mvc:annotation-driven />

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

</beans>

我正在使用注释,所以控制器看起来像

@Controller
@RequestMapping("/mailandcontact")
public class MailAndContactController {
    @RequestMapping(value="/contact", method = RequestMethod.POST)
    public ModelAndView about(HttpServletRequest request, ModelMap model)  
            // do some sork
            //create the ModelAndView , stuff in the data and return i   

【问题讨论】:

    标签: servlets spring-mvc


    【解决方案1】:

    servlet 映射

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    

    控制器

    @Controller
    @RequestMapping("/")
    public class MailAndContactController {
        @RequestMapping(value="/results", method = RequestMethod.POST)
        public String handleResults(HttpServletRequest request, Model model)  {
            //Do stuff            
            return "results";
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-24
      • 1970-01-01
      • 2021-01-13
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      • 2019-11-29
      相关资源
      最近更新 更多