当前位置: 首页 > 知识库问答 >
问题:

简单的Spring MVC、EclipseLink、Wildfly、Maven项目的设置

厍和颂
2023-03-14

我想用Maven构建一个Spring MVC webapp,它使用EclipseLink作为JPA提供程序,并在Wildfly application Server上运行。不幸的是,我不能让它运行。我想用所有东西的最新版本。

当我尝试在Wildfly8.2或Tomcat8上运行webapp时,我得到了以下错误:

下面是我的项目的文件层次结构。(我还不能在上面分享图像)

我没有打开实体包,因为我有大约30个从数据库表生成的实体。

下面是我的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>MovieTimeProject</groupId>
  <artifactId>MovieTimeProject</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>MovieTimeProject Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <repositories>
        <repository>
            <id>oss.sonatype.org</id>
            <name>OSS Sonatype Staging</name>
            <url>https://oss.sonatype.org/content/groups/staging</url>
        </repository>
        <repository>
            <id>JBoss Repository</id>
            <url>https://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
  </repositories>

  <properties>
        <spring.version>4.1.6.RELEASE</spring.version>
  </properties>

  <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jboss.spec</groupId>
            <artifactId>jboss-javaee-7.0</artifactId>
            <version>1.0.2.Final</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <!-- JUnit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.2</version>
        <scope>test</scope>
    </dependency>

    <!-- JBoss sever -->


    <!-- MySQL & JPA & EclipseLink -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.35</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.1.0</version>
        <!-- <scope>provided</scope> --> <!-- since running inside a Java EE container -->
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.6.0</version>
        <!-- <scope>provided</scope> --> <!-- Running inside a Java EE container -->
    <!-- <scope>compile</scope> -->
    </dependency>   

    <!-- Spring dependencies -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-instrument</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- Servlet & jstl -->
    <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.2-b01</version>
    </dependency>


    <dependency>
        <groupId>aopalliance</groupId>
        <artifactId>aopalliance</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>3.1</version>
    </dependency>


    <dependency>
        <groupId>aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.5.4</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.5</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

    </plugins>
    <finalName>MovieTimeProject</finalName>
  </build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="MovieTime" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <properties>
                <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
                <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/movietime2"/>
                <property name="javax.persistence.jdbc.user" value="root"/>
                <property name="javax.persistence.jdbc.password" value="root"/>
        </properties>
    </persistence-unit>
</persistence>
<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">
  <display-name>MovieTime!</display-name>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
    <!-- 
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>src/main/webapp/WEB-INF/dispatcher-servlet.xml</param-value>
    </init-param>
     -->
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <!-- <url-pattern>/</url-pattern> -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!-- 
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>src/main/webapp/WEB-INF/applicationContext.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>
    -->

</web-app>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!-- Use @Component annotations for bean definitions -->
    <context:component-scan base-package="com.movietime.controller" />

    <!-- Use @Controller annotations for MVC controller definitions -->
    <mvc:annotation-driven enable-matrix-variables="true"/>


    <!-- Add JPA support -->
     <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="loadTimeWeaver">
           <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
         </property>
     </bean>

     <!-- Add Transaction support -->
    <bean id="myTxManager"
       class="org.springframework.orm.jpa.JpaTransactionManager">
          <property name="entityManagerFactory" ref="emf"/>
    </bean>

    <!-- Use @Transaction annotations for managing transactions -->
    <tx:annotation-driven transaction-manager="myTxManager" />

    <!-- View resolver -->
    <bean class=
        "org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/"/>
    </bean>



    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/pages directory -->
    <!-- 
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>     
    </bean>
     -->

    <!-- Configure to plugin JSON as request and response in method handler -->
    <!-- 
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jsonMessageConverter"/>
            </list>
        </property>
    </bean>
      -->

    <!-- Configure bean to convert JSON to POJO and vice versa -->
    <!-- 
    <bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
     -->

</beans>
package com.movietime.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.movietime.entities.Movie;
import com.movietime.dataAccessLayer.MovieService;;

/**
 * @author Attila
 * 
 * Presentation layer (MVC) / Controller
 */
@Controller
public class HelloController {

    @Resource
    MovieService service;

    @RequestMapping("/index")
    public ModelAndView welcomePage() {

        List<Movie> result = service.findByTitle("The eternal sunshine of");
        System.out.println(result.get(0));

        return new ModelAndView("index.jsp");
    }
}
package com.movietime.dataAccessLayer;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.movietime.dao.MovieRepository;
import com.movietime.entities.Movie;

@Service
public class MovieService {

    @Resource
    private MovieRepository movieRepository;

    @Transactional(readOnly = true)
    public List<Movie> findByTitle(String title) {

        List<Movie> result = movieRepository.findMoviesByTitle(title); 
        return result;
    }
}
package com.movietime.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.movietime.entities.Movie;

@Repository
@Scope(value="singleton")
public class MovieRepository {

    @PersistenceContext
    private EntityManager em;

    @Transactional
    public void register(Movie mov) {
        // Save employee
        this.em.persist(mov);
    }


    @SuppressWarnings("unchecked")
    public List<Movie> findMoviesByTitle(String title) {

        String queryString = "SELECT m FROM Movie m WHERE m.title LIKE '" + title + "%'";
        Query query = em.createQuery(queryString, Movie.class);

        return query.getResultList();
    }

}

共有1个答案

冉锋
2023-03-14

好吧,我通过将事务类型更改为resource_local来解决这个错误。

 类似资料:
  • 我试图建立一个简单的SpringMVC项目。 我创建了Web应用程序项目,然后添加了Spring和公共日志库。 还有这个网站。xml文件 这是HelloWebservlet。xml 然后我创建了java类(HelloContoller.java)com.tutorialspoint包 然后我在WEB-INF/JSP/hello中创建了JSP页面。jsp 我正在处理服务器glassfish,当我运行

  • 注意:我是开发人员中使用JavaEE的新手。我的问题看起来很简单,但我无法理解它,我创建了一个简单的Maven项目,当我试图用Maven构建它并编写“wildfly:deploy”时,我遇到了部署“.war”的问题,这是.log: 有人能帮帮我吗

  • 嗨,evryone,我是开发JavaEE的新手,我想创建一个应用程序web,我已经创建了BD和ejb部分,其中包含包持久性、接口和服务,在war部分,我创建了包bean和webapp。。。。。我做了一个 请我需要帮助它已经4天寻找解决方案,我没有找到任何东西来帮助感谢所有

  • 我正在尝试为一个简单的EJB项目编写一个客户端,我已经在本地部署了在Eclipse中运行的Wildfly 8。 我的界面: 我的实施: 我的客户: 当我运行它时,我得到这个异常: 官方文件也是这样:https://docs.jboss.org/author/display/AS71/EJB使用JNDI从远程客户端调用 问题出在哪里? 我在帖子中谈到了boss ejb客户端。属性,但我不知道该放什么

  • 我正在使用Linux-Ubuntu14.04。我安装了詹金斯2.7.2。 我在Eclipse中创建了一个maven项目,并遵循了此链接中提供的说明http://www.guru99.com/maven-jenkins-with-selenium-complete-tutorial.html. 在完成项目设置后,我将其作为Maven项目执行,构建成功。 然后我安装了Jenkins,安装了Maven集

  • 问题内容: 我正在使用sqlite4java启动一个简单的Java测试项目,并使用Java进行构建。 我可以轻松地下载核心sqlite4java库,但是我不确定哪种最好的方式(任意!)来获取下载本地库并将它们放在正确位置的好处。 这是我的build.gradle文件: 但是,当我运行一个简单的测试时,我得到: (我是从IntelliJ内部构建的,但是我正在使用gradle构建选项-因此,我认为运行