【问题标题】:maven with spring mvc带有spring mvc的maven
【发布时间】:2012-03-26 07:03:20
【问题描述】:

有人在 Maven 中使用 Spring 进行过 Web 项目吗? 我不知道为什么我不能让我的项目工作,因此我需要查看一个工作项目来理解它。 一个使用spring mvc在maven中进行简单登录/注销的项目。

坦克

当我在服务器上运行项目时收到 HTTP 404,但我不知道可能出了什么问题。 也许我的一些 xml 文件有问题:

web.xml

  <persistence xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns  /persistence/persistence_2_0.xsd"
         version="2.0">
    <persistence-unit name="bokingguard" transaction-type="RESOURCE_LOCAL">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <!-- Entities -->
       <properties>
          <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
          <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/book"/>
          <property name="hibernate.connection.username" value=""/>
          <property name="hibernate.connection.password" value="" />
          <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
          <property name="hibernate.hbm2ddl.auto" value="update"/>
          <property name="show_sql" value="true" />
        </properties>      
    </persistence-unit>
</persistence>

spring-servlet.xml

<?xml version="1.0" encoding="windows-1252"?>
   <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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-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
   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="se.guard.domain" />
<context:component-scan base-package="se.guard.repository" />
<context:component-scan base-package="se.guard.service" />
<context:component-scan base-package="se.guard.controller" />

<!-- Use @Controller annotations for MVC controller definitions -->
<mvc:annotation-driven />

<!-- 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/" />
    <property name="suffix" value=".jsp" />
</bean>
</beans>

【问题讨论】:

  • 您应该发布您遇到的任何错误,以便我们更好地帮助您。这是一个非常模糊的问题。
  • 哪个版本的spring?登录/注销也意味着您需要弹簧安全性。
  • mkyong.com 我想它可以满足你的所有需要​​:) 祝你好运!
  • 和spring-service.xml 跨度>

标签: spring model-view-controller maven


【解决方案1】:

我在您的 web.xml 中没有看到 spring 配置。不要忘记添加弹簧配置。例如:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>
...
<servlet>
    <servlet-name>YourServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring/webmvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>YourServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
...

顺便说一句,在STS(Springsource 工具套件)中,您有一个有趣的选项:文件>新建>项目>Springsource 工具套件>Spring 模板项目。然后选择 Spring MVC 项目,您将获得一个使用 Maven 和 Spring MVC 的 HelloWorld 项目。

此外,正如 Samarth 所指出的,Spring Roo 非常适合 Spring 应用程序引导。

【讨论】:

    【解决方案2】:

    您可以使用Spring Roo 生成 Spring 推荐的项目结构,该结构使用 maven 并自动配置正确的依赖项。 您可以使用它与 Spring Security 集成以及登录和注销。

    【讨论】:

      【解决方案3】:

      使用 Spring PetClinic 或 Maven archetypes 使用 Spring MVC 构建一个工作应用程序

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-08
        • 1970-01-01
        • 2017-05-09
        • 2013-01-13
        • 1970-01-01
        • 1970-01-01
        • 2015-10-31
        相关资源
        最近更新 更多