gpt4 book ai didi

java - ViewResolver Spring 不工作

转载 作者:太空宇宙 更新时间:2023-11-04 11:28:53 27 4
gpt4 key购买 nike

我正在尝试在 Wildfly 10 服务器上使用 spring-hibernate 创建一个网站,但 View 解析器无法正常工作。当我调用 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";
}
}

最佳答案

我向你保证,你的 ViewResolver 没有损坏。问题就在这里:

@RestController
public class PaginaServiceRest {}

您的 Controller 是“完全REST”,因此此休息 Controller 内所有处理程序方法的返回结果将位于响应的主体内。

@RestController = @Controller + @ResponseBody 

简单添加@Controller并用 @ResponseBody 注释你的方法如果您希望在响应正文中返回值。

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

关于java - ViewResolver Spring 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44026854/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com