在Spring MVC中处理两个URL模式形成一个控制器

时间:2015-06-10 13:27:12

标签: spring jsp spring-mvc servlets web.xml

我在eclipse IDE中使用spring工具套件学习spring mvc。我的基础包是org.springTest.test,我在视图文件夹index.jsp和home.jsp下创建了两个jsp页面。我试图使用home.jsp作为我的欢迎页面。当我尝试访问http://localhost:8081/test/http://localhost:8081/test/index.jsp时,它总是提供index.jsp页面。我还尝试将主页添加到欢迎文件列表中。但是同样的结果。请查看网页.xml和控制器类。我怎么解决这个问题?。

HomeController.java

package org.springTest.test;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


 @Controller
 public class HomeController {


private static final Logger logger = LoggerFactory
        .getLogger(HomeController.class);


@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    logger.info("Welcome home! The client locale is {}.", locale);

    Date date = new Date();
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,
            DateFormat.LONG, locale);

    String formattedDate = dateFormat.format(date);

    model.addAttribute("serverTime", formattedDate);

    return "home";
}

@RequestMapping(value = "index", method = RequestMethod.GET)
public String home1(Locale locale, Model model) {
    logger.info("Welcome index! The client locale is {}.", locale);

    Date date = new Date();
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,
            DateFormat.LONG, locale);

    String formattedDate = dateFormat.format(date);

    model.addAttribute("serverTime", formattedDate);

    return "index";
}
}

的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">

<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>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>
    <url-pattern>/index.jsp</url-pattern>
</servlet-mapping>

</web-app>

1 个答案:

答案 0 :(得分:0)

对于spring-webmvc,如果尚未注册,则需要注册“InternalResourceViewresolver”bean。如果这已经完成,请你附上配置吗?

internalResourceViewResolver用于匹配从控制器方法到cetrain视图(jsp)的返回代码“home”和“index”。

资源视图解析器可以像这样配置:

@Bean
public InternalResourceViewResolver jspViewResolver() {
  InternalResourceViewResolver bean = new InternalResourceViewResolver();
  bean.setPrefix("/WEB-INF/views/");
  bean.setSuffix(".jsp");
  return bean;
}

它将采用您的控制器返回的字符串名称;例如“home”并将“.jsp”附加到它并查找“/WEB.INF/views/home.jsp”。 如果找不到,则抛出错误或者welcome-jsp被提供。

我还附加了servlet-context.xml和console out。

servlet的context.xml中

  <beans:bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
  </beans:bean>

<context:component-scan base-package="org.springTest.test" />

最后一行控制台输出

INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - 
    Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=           [],custom=[]}" 
    onto public java.lang.String org.springTest.test.HomeController.home
    (java.util.Locale,org.springframework.ui.Model)
INFO :        org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/index],methods=[GET],params=[],headers=[],consumes= [],produces=[],custom=[]}"onto public java.lang.String     org.springTest.test.HomeController.home1(java.util.Locale,org.springframework.ui.Model)
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - 
    Mapped URL path [/resources/**] onto handler 
    'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': 
    initialization completed in 544 ms
INFO : org.springTest.test.HomeController - Welcome index! The client     locale is en_US.