Image jpg in Spring HOW ?

0

Próbowałem wszystkiego z sieci i nadal nie działa. Pomysły ?

Wygląda to tak :
2016-12-28.png

Kody:

 package com.websystique.springmvc.configuration;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
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.websystique.springmvc")
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 addResourceHandlers(ResourceHandlerRegistry registry) {
		ResourceHandlerRegistration resourceRegistration = registry.addResourceHandler("resources/**");
		resourceRegistration.addResourceLocations("/webapp/resources/");
		registry.addResourceHandler("/gallery/**").addResourceLocations("/webapp/resources/gallery/");
		registry.addResourceHandler("/resources/**").addResourceLocations("/resources");
		registry.addResourceHandler("*.jpg").addResourceLocations("/resources/gallery/");
	}

	@Bean
	public MessageSource messageSource() {
		ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
		messageSource.setBasename("messages");
		return messageSource;
	}
}

Appinit

 package com.websystique.springmvc.configuration;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
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);
		container.addListener(new ContextLoaderListener(ctx));
		try {
			ctx.getResources("/resources/**");
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("No resources found");
		}

		ServletRegistration.Dynamic servlet = container.addServlet(
				"dispatcher", new DispatcherServlet(ctx));

		servlet.setLoadOnStartup(1);
		servlet.addMapping("/");
		servlet.addMapping("/data");
	}

}

Hibernateconfig

 package com.websystique.springmvc.configuration;

import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
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.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.websystique.springmvc.configuration" })
@PropertySource(value = { "classpath:application.properties" })
public class HibernateConfiguration {

    @Autowired
    private Environment environment;

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] { "com.websystique.springmvc.model" });
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
     }
	
    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
        dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
        dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
        dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
        return dataSource;
    }
    
    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
        properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
        properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
        return properties;        
    }
    
	@Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory s) {
       HibernateTransactionManager txManager = new HibernateTransactionManager();
       txManager.setSessionFactory(s);
       return txManager;
    }
}


appcontrol

 package com.websystique.springmvc.controller;

import java.io.InputStream;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.websystique.springmvc.model.User;
import com.websystique.springmvc.service.UserService;

@Controller
public class AppController {

	@Autowired
	UserService service;

	@Autowired
	MessageSource messageSource;
	
	@RequestMapping(value = { "/", "/index" }, method = RequestMethod.GET)
	public String home2(ModelMap model) {
		return "index";
	}
	

	@RequestMapping(value = {"/userlist" }, method = RequestMethod.GET)
	public String listUser(ModelMap model) {
		List<User> user = service.findAllUser();
		model.addAttribute("user", user);
		return "userlist";
	}

	@RequestMapping(value = { "/register" }, method = RequestMethod.GET)
	public String newUser(ModelMap model) {
		User user = new User();
		model.addAttribute("user", user);
		return "register";
	}

	@RequestMapping(value = { "/register" }, method = RequestMethod.POST)
	public String saveUser(@Valid User user, BindingResult result, ModelMap model) {
		if (result.hasErrors()) {
			return "register";
		}

		service.saveUser(user);

		model.addAttribute("success", "User " + user.getFirstName() + "Registered Successfully");
		return "success";
	}

}

index.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Koty index</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="Interactive web site">
<meta name="viewport" content="width=device-width, initial-scale=1">



</head>
<body>

<img alt="jeden" src="/resources/gallery/blacCat.jpg"/>
<img alt="jeden" src="/gallery/blacCat.jpg"/>
<img alt="jeden" src="../../resources/gallery/blacCat.jpg"/>
<img alt="jeden" src="../resources/gallery/blacCat.jpg"/>
<img alt="jeden" src="../../gallery/blacCat.jpg"/>
<a href="index.html"> <img class="logo" src="gallery/blacCat.jpg" alt="Cat logo"/></a>

<img src="<%=request.getContextPath()%>/images/logo.jpg" />
<img src="/resource/gallery/blacCat.jpg"/>
<img src="<c:url value="/gallery/blacCat.jpg"/>">
<img src="<c:url value="../gallery/blacCat.jpg"/>">
<img src="/SpringHibernateExampleweb/src/main/webapp/resources/gallery/blacCat.jpg" />

<spring:url value="/resources/gallery" var="images" />
    <img src="${images}/blacCat.jpg"/>

</body>
</html> 
0

Miałem podobny problem, bo nie miałem folderu STATIC w resource.
Utwórz foldery /resource/static/gallery i spróbuj odwołać się poprzez src="/gallery....

1 użytkowników online, w tym zalogowanych: 0, gości: 1