ViewResolver没有解析查看名称,而是请求URL,Spring 3.0

时间:2014-07-27 04:03:22

标签: java spring jsp

viewresolver正在解析请求url而不是返回的视图名称,在这里帮助我,因为我是Spring的新手

日志显示:

MethodName : springController()

ClassName : ModelAndView: reference to view with name 'StartPage'; model is {message=Hey welcome to Spring 3.0!! }
-------------------------------------------------------------------------------
MethodName : springContoller

Invoking afterPropertiesSet() on bean with name 'home'
Rendering view [org.springframework.web.servlet.view.JstlView: name 'home'; URL [/jsp/home.jsp]] in DispatcherServlet with name 'spring'
Forwarding to resource [/jsp/home.jsp] in InternalResourceView 'home'
Successfully completed request

bean config:

<bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

控制器:

@Controller
public class HomeController {

    @Autowired
    private ObjectMapper objmapper;

    @Autowired
    public SLogger tLogger;

    @RequestMapping("/home")
    public ModelAndView springContoller(){
        String message = "Hey welcome to Spring 3.0!! ";
        System.out.println("in controller");
        tLogger.writeToLog(HomeController.class.getName(), "springController()", Level.DEBUG);
        return new ModelAndView("StartPage","message",message);
    }
    }

不是解析为/jsp/StartPage.jsp而是解析为/jsp/home.jsp,为什么?

弹簧servlet.xml中

<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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

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

    <aop:aspectj-autoproxy/>

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

    <bean id="objectMapper" class="org.codehaus.jackson.map.ObjectMapper">
    </bean>

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="file:D:/galilio/siso.properties"></property>
    </bean>

   <bean id="sLogger"
            class="siso.springrolls.utilities.SLogger" factory-method="getLogger">
            <property name="iLogger" ref="iLogger"></property>
   </bean> 

    <bean id="iLogger" class="org.apache.log4j.Logger" factory-method="getLogger">
    <constructor-arg type="java.lang.String" value="ILogger"></constructor-arg>
    </bean>

    <bean id="logAspect" class="siso.springrolls.utilities.LoggingAspect"></bean>

</beans>

web.xml:

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

<listener>  
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener> 

3 个答案:

答案 0 :(得分:1)

我有同样的问题,解决方案非常简单 - 而不是导入包

import org.springframework.web.servlet.ModelAndView;

您已导入

import org.springframework.web.portlet.ModelAndView;

答案 1 :(得分:0)

这是解决方法,但它会起作用。 做如下 在web.xml中

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

在你的HomeController中

//first landing controller
 //it will redirect to your home controller
 @RequestMapping(value = "/", method = RequestMethod.GET)
 public ModelAndView home() {
    return new ModelAndView("redirect:/home")
 }

 @RequestMapping("/home")
 public ModelAndView springContoller(){
    String message = "Hey welcome to Spring 3.0!! ";
    System.out.println("in controller");
    tLogger.writeToLog(HomeController.class.getName(), "springController()",
    Level.DEBUG);
    return new ModelAndView("StartPage","message",message);
 }

如果添加url-pattern as /home,这意味着servlet将仅映射/ home目录。 所以我们改为/

答案 2 :(得分:0)

我添加<aop:aspectj-autoproxy/>时遇到类似问题。从配置文件中删除后,一切都很好。