简体   繁体   English

基于Spring MVC Annotations的Hello World不起作用

[英]Spring MVC Annotations Based Hello World Doesn't Work

I've been trying now for about a week to get a simple Spring hello world style project running without any luck.. so I'm hoping SO can help. 我现在已经尝试了大约一个星期才能得到一个简单的Spring hello世界风格的项目,没有任何运气。所以我希望能帮助你。

I'm trying to access this URL (localhost:8080/test/welcome) to display "Hello World!". 我正在尝试访问此URL(localhost:8080 / test / welcome)以显示“Hello World!”。

My aim is to use Spring annotations instead of configuring the beans via xml. 我的目标是使用Spring注释而不是通过xml配置bean。

Currently when I access the above URL I get a HTTP Status 404 (the requested resource is not available) error. 目前,当我访问上述URL时,我收到HTTP状态404(请求的资源不可用)错误。

These are the files I have: 这些是我的文件:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="3.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>test</display-name>
    <!-- Spring MVC -->
    <servlet>
        <servlet-name>test-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>test-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/test-dispatcher-servlet.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

test-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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">
    <context:component-scan base-package="com.test.test.controllers"/>
    <mvc:annotation-driven />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

WelcomeController.java

package com.test.test.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/welcome")
public class WelcomeController {

    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(Model model) {

        return "welcome";
    }
}

welcome.jsp

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

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>com.test</groupId>
    <artifactId>test</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>test</name>
    <build>
        <finalName>test</finalName>
    </build>
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <org.springframework.version>3.2.3.RELEASE</org.springframework.version>
        <org.springframework.security.version>3.1.4.RELEASE</org.springframework.security.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
        </dependency>
        <!-- Spring Dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <!-- Spring Security Dependencies -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${org.springframework.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${org.springframework.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${org.springframework.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-acl</artifactId>
            <version>${org.springframework.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>${org.springframework.security.version}</version>
        </dependency>
    </dependencies>
</project>

My project structure: 我的项目结构: 项目文件夹结构

I just can't figure out why it isn't working and I'm not sure if I can even strip it down any further than I already have... 我只是无法弄清楚它为什么不起作用,我不确定我是否能够将它剥离得比我已经拥有的更多......

I have so many questions about Spring but I guess they will have to wait for another question. 我有很多关于Spring的问题,但我想他们将不得不等待另一个问题。 This experience so far has been more challenging than anything else I've done... and it's just to set it up! 到目前为止,这种体验比我做过的任何其他事情都更具挑战性......而且只是设置它! I can't wait to get to the actual programming! 我迫不及待想要进行实际编程!

Thanks for any help. 谢谢你的帮助。

Is there a reason why you put your controller in the src/test/java folder? 是否有理由将控制器放在src/test/java文件夹中? That folder is, as far as I know, skipped during packaging, so your WAR-file doesn't contain a controller anymore when you package it. 据我所知,该文件夹在打包过程中被跳过,因此当您打包它时,您的WAR文件不再包含控制器。

Create a folder called src/main/java . 创建一个名为src/main/java的文件夹。 I notice you're using the Maven plugin so normally it will automatically be picked up as a source folder. 我注意到你正在使用Maven插件,所以通常它会自动被选为源文件夹。 If not, right click your project and go to Maven and then Update configuration (or something similar). 如果没有,请右键单击您的项目并转到Maven ,然后更新配置 (或类似的东西)。

Now move the package com.test.test.controller to the src/main/java folder and build + deploy again. 现在将包com.test.test.controller移动到src/main/java文件夹并再次构建+部署。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM