[英]Spring 4.3.5 annotation based configuration tomcat-8 404 in eclipse neon 2
I have tried lot of time the same project to develop spring 4 based web app, but i am able to a project single time. 我已经花了很多时间在同一个项目上,以开发基于Spring 4的Web应用程序,但是我能够一次完成一个项目。 I don't understand where i am making mistake.
我不明白我在哪里犯错。 I have taken lot of references for my first Spring 4 annotation based first web application and i have followed each step by step.
我已经为第一个基于Spring 4注解的第一个Web应用程序获取了很多参考资料,并且我已经一步一步地遵循了。 But all the time i failed.
但是我一直都失败了。 No i am here to understand where i am making mistake.
不,我在这里了解我在哪里犯错。
Please friend review and let me know the issue. 请朋友审查,并让我知道这个问题。
Efforts 努力
Firstly i have create maven project and used maven-archtypes-webapp
and specify group-ip, packages and so on. 首先,我创建了maven项目,并使用了
maven-archtypes-webapp
并指定了group-ip,packages等。
Then, i have add dependencies and plugin, here is my pom.xml
file. 然后,我添加了依赖项和插件,这是我的
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.org.spring4</groupId>
<artifactId>FirstApp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>FirstApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.5.RELEASE</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.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>firstapp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warSourceDirectory>src/main/webapp/</warSourceDirectory>
<warName>firstapp</warName>
</configuration>
</plugin>
</plugins>
</build>
</project>
Then i have created annotation based configuration as mentioned on http://websystique.com/ this website. 然后,我创建了基于注释的配置,如http://websystique.com/此网站上所述。 i have not created [it's the preferred way] class, instead i have used first two classes.
我还没有创建[这是首选方式]类,而是使用了前两个类。
here are both configuration classes: 这是两个配置类:
AppConfig.java
package com.org.spring.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.org.spring")
public class AppConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
and AppInitializer.java 和AppInitializer.java
package com.org.spring.config;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class AppInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
ctx.setServletContext(container);
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
}
after that i have created my controller as, 之后,我创建了我的控制器,
package com.org.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("/hello")
public String hello(Model model) {
model.addAttribute("name", "John Doe");
return "welcome";
}
}
then, i have created welcome.jsp file in WEB-INF/views directory as per my configuration. 然后,根据我的配置,我已经在WEB-INF / views目录中创建了welcome.jsp文件。
When i tried to run this project on my tomcat 8 it gives me 404 error always. 当我尝试在tomcat 8上运行此项目时,始终会出现404错误。 I don't know why i am getting this error.
我不知道为什么我会收到此错误。
Please friend help me. 请朋友帮我。
将@Controller更改为@RestController并将@RequestMapping添加到您的类中,以访问您的api localhost:port / uri / apimapping / hello
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.