ViewResolver Spring无法运行

时间:2017-05-17 13:52:08

标签: java spring hibernate jsp servlets

我正在尝试使用spring-hibernate在wildfly 10服务器上创建一个网站,但视图解析器无效。 当我调用servlet时,它会回答一个简单的字符串“prova1”,我认为它会进入“prova1.jsp”,而不是简单地在浏览器中写入prova1。 我试过,作为从servlet上下文中删除viewresolver定义的测试,基本上没有任何改变。

有什么想法吗?

这是servlet上下文

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    <!-- Enable annotation-based Spring MVC controllers (eg: @Controller annotation) -->
    <mvc:annotation-driven />


    <mvc:default-servlet-handler/>

    <context:annotation-config />
    <!-- Classpath scanning of @Component, @Service, etc annotated class -->
    <context:component-scan base-package="it.zeurelscan.dao.impl" />
    <context:component-scan base-package="it.zeurelscan.serviceImpl" />
    <context:component-scan base-package="it.zeurelscan.serviceRest" />
    <context:component-scan base-package="it.zeurelscan.service" />
    <context:component-scan base-package="it.zeurelscan.model" />

    <!-- Resolve view name into jsp file located on /WEB-INF -->
    <!-- <bean -->
    <!-- class="org.springframework.web.servlet.view.InternalResourceViewResolver"> -->

    <!-- <property name="prefix" value="/WEB-INF/views/" /> -->
    <!-- <property name="suffix" value=".jsp" /> -->
    <!-- </bean> -->

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


    <!-- EntityManagerFactory -->
    <bean
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        id="entityManagerFactory">
        <property name="persistenceUnitName" value="zeurelscan" />
    </bean>

    <!-- Vedi https://techannotation.wordpress.com/2012/10/04/spring-jta-with-jboss/ -->
    <bean id="txManager"
        class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManagerName" value="java:jboss/TransactionManager" />
        <property name="userTransactionName" value="java:jboss/UserTransaction" />
    </bean>

    <!-- Enable @Transactional annotation -->
    <tx:annotation-driven />
    <!-- <tx:annotation-driven transaction-manager="txManager" /> -->
    <!-- <tx:jta-transaction-manager /> -->
    <bean id="transactionManager"
        class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManagerName" value="java:/TransactionManager" />
    </bean>

    <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
    <mvc:resources mapping="/static/**" location="/static/" />

</beans>

这是web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <context-param>
    <param-name>contextConfigLocation</param-name>
<!--     <param-value>/WEB-INF/spring/root-context.xml, /WEB-INF/spring/appServlet/servlet-security.xml</param-value> -->
<param-value>/WEB-INF/spring/root-context.xml</param-value>
  </context-param>
  <context-param>
    <param-name>PARAMETER_ENCODING</param-name>
    <param-value>UTF-8</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>


  <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>
  </servlet-mapping>

    <welcome-file-list>
        <welcome-file></welcome-file>
    </welcome-file-list>


<!--      <welcome-file-list> -->
<!--         <welcome-file>/WEB-INF/views/index.jsp</welcome-file> -->
<!--     </welcome-file-list> -->

<!--      <servlet> -->
<!--        <servlet-name>jsp</servlet-name> -->
<!--        <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class> -->
<!--    </servlet> -->

</web-app>

这是servlet

package it.zeurelscan.serviceRest;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import it.zeurelscan.model.Pagina;
import it.zeurelscan.service.PaginaService;
@RestController
public class PaginaServiceRest {

    @Autowired
    PaginaService immaginiService;  

    private Logger logger = Logger.getLogger(PaginaServiceRest.class);

    @RequestMapping(value = "/pagine2",method=RequestMethod.GET)
    public String immagini2(){
        return "prova1";
    }
}

1 个答案:

答案 0 :(得分:0)

我向你保证,你的ViewResolver没有被打破。问题出在这里:

@RestController
public class PaginaServiceRest {}

你的控制器是&#34;完全REST&#34;所以这个rest-controller里面的所有处理程序方法的返回结果都在于响应体内。

@RestController = @Controller + @ResponseBody 

如果您希望在回复正文中返回值,只需添加@Controller并使用@ResponseBody注释您的方法。

您也可以从servlet.xml中删除此行<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />,因为它不是必需的。