有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

启动时未找到java Spring MVC资源

所以我知道有很多类似的帖子,但不幸的是,这些帖子都没有帮到我。我只是想让一个演示SpringMVC项目启动并运行。我正在尝试运行Heroku(https://devcenter.heroku.com/articles/getting-started-with-heroku-eclipse)提供的模板项目。我尝试了许多设置组合,但都没有效果。以下是默认设置:

网络。xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Spring-Hibernate-Template</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>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/people/*</url-pattern>
</servlet-mapping>
</web-app>

应用程序上下文。xml:

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

<context:annotation-config />
<context:component-scan base-package="com.example" />

<mvc:annotation-driven/>

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

<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <property name="dataSource" ref="dataSource"/>

</bean>

<beans profile="default">
    <jdbc:embedded-database id="dataSource"/>        
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>
</beans>

<beans profile="prod">
    <bean class="java.net.URI" id="dbUrl">
        <constructor-arg value="#{systemEnvironment['DATABASE_URL']}"/>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="url" value="#{ 'jdbc:postgresql://' + @dbUrl.getHost() + ':' + @dbUrl.getPort() + @dbUrl.getPath() }"/>
        <property name="username" value="#{ @dbUrl.getUserInfo().split(':')[0] }"/>
        <property name="password" value="#{ @dbUrl.getUserInfo().split(':')[1] }"/>
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <!-- change this to 'verify' before running as a production app -->
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>
</beans>

</beans>

人事控制员。爪哇:

@Controller
public class PersonController {

    @Autowired
    private PersonService personService;

    @RequestMapping("/")
    public String listPeople(Map<String, Object> map) {

        map.put("person", new Person());
        map.put("peopleList", personService.listPeople());

        return "people";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addPerson(@ModelAttribute("person") Person person, BindingResult result) {

        personService.addPerson(person);

        return "redirect:/people/";
    }

    @RequestMapping("/delete/{personId}")
    public String deletePerson(@PathVariable("personId") Integer personId) {

        personService.removePerson(personId);

        return "redirect:/people/";
    }
}

我有一个“人”。位于webapp/WEB-INF/jsp/people/jsp的jsp文件

我的服务器。Tomcat服务器的xml上下文元素如下所示:

<Context docBase="facultypublicationsdb" path="/facultypublicationsdb" reloadable="true" source="org.eclipse.jst.jee.server:facultypublicationsdb"/></Host>

每次我在(http://localhost:8080/facultypublicationsdb/)的Tomcat上运行这个时,我都会得到以下结果:

HTTP Status 404 - /facultypublicationsdb/

type Status report

message /facultypublicationsdb/

description The requested resource (/facultypublicationsdb/) is not available.

Apache Tomcat/7.0.21

我通过eclipse在Ubuntu上运行这个。我注意到没有。war正在被复制到/usr/share/tomcat7/webapps目录。这应该发生吗

有什么想法吗


共 (2) 个答案

  1. # 1 楼答案

    我认为您需要在listPeople方法中指定method = RequestMethod.GET。可能有许多小事情会导致404 error。你能把这段代码上传到GitHub上吗?我会调查的

  2. # 2 楼答案

    试着检查两件事

    首先改变你的网站。将dispatcher servlet映射到/的xml文件。这导致在找不到请求的其他映射而不是每个请求的映射时使用DispatcherServlet。如果您有CSS和Javascript等资源,这一点很重要

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/people/*</url-pattern>
    </servlet-mapping>
    

    接下来,如果使用Eclipse,请打开项目属性(在项目浏览器中单击project,Alt+Enter)。然后转到部署程序集。确保所有项目资源都包含在这里,尤其是任何maven依赖项。如果注意到缺少依赖项,请单击“添加”按钮并选择它们

    您可能还希望确保通过组件扫描拾取控制器。确保控制器位于com.example包中