我试图将我的Springmvc项目转换为Spring启动。我根据Spring启动转换了所有必要的文件。控制台上没有错误。但是当我在浏览器中运行我的网络应用程序时,我得到了这个错误。
在名为“DispatcherServlet”的DispatcherServlet中找不到URI为[/onlineshopping/WEB-INF/views/page.jsp]的HTTP请求的映射
我尝试运行的任何url都会收到相同的错误,为什么?
网上购物应用。JAVA
package net.kzn.onlineshopping;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@SpringBootApplication
@EnableWebSecurity
@ImportResource({ "classpath:/**/spring-security.xml" })
@ComponentScan(basePackages = { "net.kzn.onlineshopping.*", "net.kzn.shoppingbackend.*" })
public class OnlineshoppingApplication {
public static void main(String[] args) {
SpringApplication.run(OnlineshoppingApplication.class, args);
}
}
控制台日志文件链接
https://www.dropbox.com/s/6pk0bq2xn1jzmqr/error.txt?dl=0
AppConfig.java
package net.kzn.onlineshopping.config;
import java.util.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.ui.context.support.ResourceBundleThemeSource;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.PathResourceResolver;
import org.springframework.web.servlet.theme.CookieThemeResolver;
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.webflow.config.AbstractFlowConfiguration;
import org.springframework.webflow.definition.registry.FlowDefinitionRegistry;
import org.springframework.webflow.engine.builder.support.FlowBuilderServices;
import org.springframework.webflow.executor.FlowExecutor;
import org.springframework.webflow.mvc.builder.MvcViewFactoryCreator;
import org.springframework.webflow.mvc.servlet.FlowHandlerMapping;
@Configuration
@EnableWebMvc
@ComponentScan("net.kzn.onlineshopping")
public class AppConfig extends AbstractFlowConfiguration implements WebMvcConfigurer {
@Autowired
private AppConfig AppConfig;
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}
/** View resolver for JSP */
@Bean
public ViewResolver getViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
/** Multipart file uploading configuratioin */
@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(100000);
return multipartResolver;
}
/** Static resource locations including themes*/
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/assets/");
}
/** BEGIN theme configuration */
@Bean
public ResourceBundleThemeSource themeSource(){
ResourceBundleThemeSource themeSource = new ResourceBundleThemeSource();
themeSource.setDefaultEncoding("UTF-8");
themeSource.setBasenamePrefix("themes.");
return themeSource;
}
@Bean
public CookieThemeResolver themeResolver(){
CookieThemeResolver resolver = new CookieThemeResolver();
resolver.setDefaultThemeName("default");
resolver.setCookieName("example-theme-cookie");
return resolver;
}
@Bean
public ThemeChangeInterceptor themeChangeInterceptor(){
ThemeChangeInterceptor interceptor = new ThemeChangeInterceptor();
interceptor.setParamName("theme");
return interceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(themeChangeInterceptor());
}
/** END theme configuration */
@Bean
public FlowDefinitionRegistry flowRegistry() {
return getFlowDefinitionRegistryBuilder()
.setBasePath("/WEB-INF/views/flows")
.addFlowLocationPattern("/**/*-flow.xml")
.build();
}
@Bean
public FlowExecutor flowExecutor() {
return getFlowExecutorBuilder(flowRegistry()).build();
}
@Bean
public FlowBuilderServices flowBuilderServices() {
return getFlowBuilderServicesBuilder().setViewFactoryCreator(mvcViewFactoryCreator()).setDevelopmentMode(true).build();
}
@Bean
public FlowHandlerMapping flowHandlerMapping() {
FlowHandlerMapping handlerMapping = new FlowHandlerMapping();
handlerMapping.setOrder(-1);
handlerMapping.setFlowRegistry(this.AppConfig.flowRegistry());
return handlerMapping;
}
@Bean
public MvcViewFactoryCreator mvcViewFactoryCreator() {
MvcViewFactoryCreator factoryCreator = new MvcViewFactoryCreator();
factoryCreator.setViewResolvers(Collections.singletonList(this.AppConfig.getViewResolver()));
factoryCreator.setUseSpringBeanBinding(true);
return factoryCreator;
}
}
WebAppInitializer。JAVA
package net.kzn.onlineshopping.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 WebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(AppConfig.class);
context.setServletContext(container);
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(context));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
}
应用属性
server.port=8081
logging.path=/home/vidyesh/Downloads
server.servlet.context-path=/onlineshopping
波姆。xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.kzn</groupId>
<artifactId>shoppingbackend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>shoppingbackend</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.17.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-hal-browser</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.webflow/spring-webflow -->
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-webflow</artifactId>
<version>2.5.1.RELEASE</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.2.2</version>
</dependency>
<!-- Hibernate Dependency -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.7.Final</version>
</dependency>
<!-- database connection pooling -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/webapp</directory>
</resource>
</resources>
</build>
</project>
页面控制器。JAVA
package net.kzn.onlineshopping.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import net.kzn.onlineshopping.exception.ProductNotFoundException;
import net.kzn.shoppingbackend.dao.CategoryDAO;
import net.kzn.shoppingbackend.dao.ProductDAO;
import net.kzn.shoppingbackend.dto.Category;
import net.kzn.shoppingbackend.dto.Product;
@Controller
public class PageController {
private static final Logger logger = LoggerFactory.getLogger(PageController.class);
@Autowired
private CategoryDAO categoryDAO;
@Autowired
private ProductDAO productDAO;
@RequestMapping(value = { "/", "/home", "/index" })
public ModelAndView index(@RequestParam(name = "logout", required = false) String logout) {
ModelAndView mv = new ModelAndView("page");
mv.addObject("title", "Home");
logger.info("Inside PageController index method - INFO");
logger.debug("Inside PageController index method - DEBUG");
// passing the list of categories
mv.addObject("categories", categoryDAO.list());
if (logout != null) {
mv.addObject("message", "You have successfully logged out!");
}
mv.addObject("userClickHome", true);
return mv;
}
@RequestMapping(value = "/about")
public ModelAndView about() {
ModelAndView mv = new ModelAndView("page");
mv.addObject("title", "About Us");
mv.addObject("userClickAbout", true);
return mv;
}
@RequestMapping(value = "/contact")
public ModelAndView contact() {
ModelAndView mv = new ModelAndView("page");
mv.addObject("title", "Contact Us");
mv.addObject("userClickContact", true);
return mv;
}
/*
* Methods to load all the products and based on category
*/
@RequestMapping(value = "/show/all/products")
public ModelAndView showAllProducts() {
ModelAndView mv = new ModelAndView("page");
mv.addObject("title", "All Products");
// passing the list of categories
mv.addObject("categories", categoryDAO.list());
mv.addObject("userClickAllProducts", true);
return mv;
}
@RequestMapping(value = "/show/category/{id}/products")
public ModelAndView showCategoryProducts(@PathVariable("id") int id) {
ModelAndView mv = new ModelAndView("page");
// categoryDAO to fetch a single category
Category category = null;
category = categoryDAO.get(id);
mv.addObject("title", category.getName());
// passing the list of categories
mv.addObject("categories", categoryDAO.list());
// passing the single category object
mv.addObject("category", category);
mv.addObject("userClickCategoryProducts", true);
return mv;
}
/*
* Viewing a single product
*/
@RequestMapping(value = "/show/{id}/product")
public ModelAndView showSingleProduct(@PathVariable int id) throws ProductNotFoundException {
ModelAndView mv = new ModelAndView("page");
Product product = productDAO.get(id);
if (product == null)
throw new ProductNotFoundException();
// update the view count
product.setViews(product.getViews() + 1);
productDAO.update(product);
// ---------------------------
mv.addObject("title", product.getName());
mv.addObject("product", product);
mv.addObject("userClickShowProduct", true);
return mv;
}
@RequestMapping(value = "/membership")
public ModelAndView register() {
ModelAndView mv = new ModelAndView("page");
logger.info("Page Controller membership called!");
return mv;
}
@RequestMapping(value = "/login")
public ModelAndView login(@RequestParam(name = "error", required = false) String error,
@RequestParam(name = "logout", required = false) String logout) {
ModelAndView mv = new ModelAndView("login");
mv.addObject("title", "Login");
if (error != null) {
mv.addObject("message", "Username and Password is invalid!");
}
if (logout != null) {
mv.addObject("logout", "You have logged out successfully!");
}
return mv;
}
@RequestMapping(value = "/logout")
public String logout(HttpServletRequest request, HttpServletResponse response) {
// Invalidates HTTP Session, then unbinds any objects bound to it.
// Removes the authentication from securitycontext
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null) {
new SecurityContextLogoutHandler().logout(request, response, auth);
}
return "redirect:/login?logout";
}
@RequestMapping(value = "/access-denied")
public ModelAndView accessDenied() {
ModelAndView mv = new ModelAndView("error");
mv.addObject("errorTitle", "Aha! Caught You.");
mv.addObject("errorDescription", "You are not authorized to view this page!");
mv.addObject("title", "403 Access Denied");
return mv;
}
@RequestMapping(value = "/view/category/{id}/products")
public ModelAndView viewProducts(@PathVariable("id") int id) {
ModelAndView mv = new ModelAndView("page");
// categoryDAO to fetch a single category
Category category = null;
category = categoryDAO.get(id);
mv.addObject("title", category.getName());
// passing the list of categories
mv.addObject("viewproducts", productDAO.listActiveProductsByCategory(id));
mv.addObject("userClickViewProducts", true);
return mv;
}
@RequestMapping(value = "/search")
public ModelAndView Search(@RequestParam(value = "searchTerm", required = false) String pSearchTerm,
HttpServletRequest request, HttpServletResponse response) {
ModelAndView mv = new ModelAndView("search");
mv.addObject("searchTerm", pSearchTerm);
mv.addObject("searchResult", productDAO.searchProductsByParam(pSearchTerm));
mv.addObject("userClickSearch", true);
return mv;
}
}
请告诉我我做错了什么?
您的视图解析器似乎没有用于处理JSP的viewclass。尝试在AppConfig中设置viewclass。JAVA
/** View resolver for JSP */
@Bean
public ViewResolver getViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
添加此配置后,我的问题解决了
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
添加此配置后,白页错误停止,但jsp页面代码开始显示为html,因此添加了这两个依赖项。
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>provided</scope>
这个链接有帮助
未在Spring Boot web应用程序中呈现JSP文件
我写了一个spring boot项目。它有三个文件。 appconfig.java HelloController.java 当我尝试运行它时,它出现了错误“没有为名为'DispatcherServlet中URI[/springc1_01/]的HTTP请求找到映射”。这是因为服务器没有找到控制器还是其他原因?THX.
从控制器返回Html页面时获取此错误 org.springframework.web.servlet.页未找到noHandler已找到警告:没有找到HTTP请求与URI[/SpringMVCDemo/WEB-INF/response.html]在DispatcherServlet名称调度器映射 放在哪里response.html? 这是我的密码
这是我的网站。xml 我的servlet上下文。xml 最后是Handler类。这是在com下。比如Spring。控制器。恳求 但是,我们将转到 它返回404错误。 这是我的项目结构
我使用的是从web下载的代码,是Spring MVC Hibernate Maven Spring数据的一个示例,在这段代码中,页面是jsp。现在我想介绍Thymeleaf而不是jsp si,我已经创建了一个HTML问候页面,但是当我进入这个页面时,我得到了这个错误 09:16:23.622[http-nio-8080-exec-7]警告o.s.web。servlet。PageNotFound-在
很抱歉再次问这种问题,但我无法通过查看其他线程和Spring doc来解决我的问题。 我正在使用maven的3.1.0.RELEASE,并尝试使用注释和java配置。 以下是我的web.xml: 这是我的档案web-application-config.xml. 我有两个类。第一个配置视图解析器 第二个定义我的控制器: 根据我的配置,我想一切都应该指向我的home()函数。然而,事实并非如此,以下
Dispatcher servlet(servlet-context.xml) http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xs