Spring MVC java based configuration without xml not working - spring-mvc

I am trying to create sample spring MVC application with java based configuration with annotation so I am not using any XML file for configuration.
I have below Controller, Dispatcher servlet initializer class.
My JSP file is home.jsp which is present inside WEB-INF/view folder. Kindly help.
#Controller
public class DemoController {
#RequestMapping("/")
public String showHome(Model model) {
return "home";
}
}
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = "com.luv2code.springsecurity")
public class DemoAppConfig {
// define a bean for ViewResolver
#Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
public class MySpringMvcDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return null;
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{DemoAppConfig.class};
}
#Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}

Related

jquery datatables with thymleaf and <style> tag issue

My issue is I have a jquery datatables in my page which belongs to a web app using springboot 2.3.2.RELEASE and Java 11 and Thymleaf.
In order to have the same behaviour as the following : Jquery datatables Row details
I have my png files as follows :
and in my Configuration class :
#Configuration
#EnableWebMvc
public class MvcConfig extends WebMvcConfigurationSupport {
#Bean
static PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
#Override
public RequestMappingHandlerMapping createRequestMappingHandlerMapping() {
RequestMappingHandlerMapping requestMappingHandlerMapping = super.createRequestMappingHandlerMapping();
requestMappingHandlerMapping.setUseTrailingSlashMatch(false);
return requestMappingHandlerMapping;
}
#Bean(name = "messageSource")
public MessageSource configureMessageSource() {
// default message source
ReloadableResourceBundleMessageSource defaultMessageSource = new ReloadableResourceBundleMessageSource();
defaultMessageSource.setBasename("classpath:i18n/messages");
defaultMessageSource.setDefaultEncoding("UTF-8");
defaultMessageSource.setCacheSeconds(5000);
return defaultMessageSource;
}
#Bean
public LocaleResolver localeResolver() {
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setDefaultLocale(new Locale("fr"));
resolver.setCookieName("localeCookie");
resolver.setCookieMaxAge(5000);
return resolver;
}
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
#Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
interceptor.setParamName("lang");
registry.addInterceptor(interceptor);
}
#Bean
public ClassLoaderTemplateResolver templateResolver() {
ClassLoaderTemplateResolver result = new ClassLoaderTemplateResolver();
result.setPrefix("/templates/");
result.setSuffix(".html");
result.setTemplateMode("HTML");
result.setOrder(1);
result.setCacheable(true);
return result;
}
#Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
// templateEngine.addDialect(new SpringSecurityDialect());
templateEngine.addDialect(new nz.net.ultraq.thymeleaf.LayoutDialect());
return templateEngine;
}
#Bean
public ThymeleafViewResolver viewResolver() {
ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
thymeleafViewResolver.setTemplateEngine(templateEngine());
thymeleafViewResolver.setCharacterEncoding("UTF-8");
return thymeleafViewResolver;
}
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
#Override
protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
}
#Bean
public MultipartResolver multipartResolver() {
return new StandardServletMultipartResolver();
}
}
In developer console I have 404 on the link http://localhost:8080/tsp/details_open.png
I'm struggling in vain. Unless I'm mistaken, It's related to static resources handling.
The tsp is coming from the following application.yml file :
server:
port: 8080
servlet:
context-path: /tsp
I have made a seperate test projet, isolated the issue which was my MvcConfig
Any help please ?
I fixed my issue by updating the MvcConfig class as follows :
#Configuration
#EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
...
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(
"/img/**",
"/webfonts/**",
"/css/**",
"/js/**")
.addResourceLocations(
"classpath:/static/img/",
"classpath:/static/webfonts/",
"classpath:/static/css/",
"classpath:/static/js/");
}
#Bean
public ClassLoaderTemplateResolver templateResolver() {
ClassLoaderTemplateResolver result = new ClassLoaderTemplateResolver();
result.setPrefix("/templates/");
result.setSuffix(".html");
result.setTemplateMode(TemplateMode.HTML);
result.setOrder(1);
result.setCacheable(false);
return result;
}
...
}
Which means, I changed WebMvcConfigurationSupport by WebMvcConfigurer and delete useless custom code.
What is weired is that, according to spring documentation, static folder should be automatically loaded...but it's not...this is why I added the resouce handlers.
To optimize, the performance, I copied all js/css/map files locally.

Spring mvc cannot autowire dao classes in junit tests

I have a spring mvc web application configured using javaconfig and I am trying to autowire my dao classes on the junit test class and I am using #EnableWebMvc for the servlet config, but it gives me the following exception:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.jahidul.islam.test.tests.TestJunit': Unsatisfied dependency expressed through field 'strengthDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.jahidul.islam.dao.StrengthDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
My Test Class which is used for the junit test:
#RunWith(SpringJUnit4ClassRunner.class)
#WebAppConfiguration
#ContextHierarchy({
#ContextConfiguration(loader=AnnotationConfigWebContextLoader.class),
#ContextConfiguration(classes=WebAppInitializer.class)
})
public class TestJunit {
#Autowired
WebApplicationContext context;
#Autowired
StrengthDao strengthDao;
#Test
public void test() {
System.out.println(strengthDao.getStrength(1));
}
}
My WebAppInitializer Class which initializes all the configurations:
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {WebAppBeanConfig.class, WebAppDaoConfig.class, WebAppServiceConfig.class, WebSecurityConfig.class};
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {WebAppServletConfig.class};
}
#Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
#Override
protected Filter[] getServletFilters() {
return new Filter[]{new HiddenHttpMethodFilter()};
}
#Override
protected DispatcherServlet createDispatcherServlet(WebApplicationContext servletAppContext) {
final DispatcherServlet dispatcherServlet = (DispatcherServlet) super.createDispatcherServlet(servletAppContext);
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
return dispatcherServlet;
}
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
servletContext.setInitParameter("spring.profiles.active", "production");
}
}
My Dao Class which is used to fetch data from mysql database using hibernate:
#Component("strengthDao")
#Transactional
public class StrengthDao {
#Autowired
private SessionFactory sessionFactory;
private Session session() {
return sessionFactory.getCurrentSession();
}
public List<Strength> getAllStrengths(int perPage, int offset) {
CriteriaBuilder builder = session().getCriteriaBuilder();
CriteriaQuery<Strength> criteriaQuery = builder.createQuery(Strength.class);
Root<Strength> root = criteriaQuery.from(Strength.class);
criteriaQuery.select(root);
return (List<Strength>) session().createQuery(criteriaQuery).setFirstResult(offset).setMaxResults(perPage).getResultList();
}
public long strengthCount() {
CriteriaBuilder builder = session().getCriteriaBuilder();
CriteriaQuery<Long> criteriaQuery = builder.createQuery(Long.class);
Root<Strength> root = criteriaQuery.from(Strength.class);
criteriaQuery.select(builder.count(root));
return (long) session().createQuery(criteriaQuery).getSingleResult();
}
public void saveOrUpdateStrength(Strength strength) {
session().saveOrUpdate(strength);
}
public Strength getStrength(int id) {
return (Strength) session().get(Strength.class, id);
}
public void deleteStrength(Strength strength) {
session().delete(strength);
}
}
Either add componentScan annotation to TestJunit class to auto detect #Repository components.
Or create a java config bean in TestJunit class using #Bean annotation like
#Bean
public StrengthDao StrengthDao(){
return new StrengthDao ();
}

How to implement session management in spring with time period 30 minute

In my post i have tried simple login functionality whether the user is valid or not. if the user is valid i have to implement session feature for 30 min,
if the user is not doing anything till 30 min how to make him to logout, he has to login with username and password. can anyone explain how i can implement session management in spring mvc with the above requirement session time for 30 min ?
AppConfig
#Configuration
#PropertySource("classpath:spring.properties")
#EnableWebMvc
#ComponentScan(basePackages = "com.spring")
public class AppConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
#Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
#Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource rb = new ResourceBundleMessageSource();
rb.setBasenames(new String[] { "messages/messages", "messages/validation" });
return rb;
}
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
AppInitializer
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("/");
}
}
Contoller
#RequestMapping(value = "/authenticateUserLogin", method = RequestMethod.POST)
public ModelAndView authenticateUserLogin(#ModelAttribute("loginForm") #Validated User user, BindingResult result, Model model, final RedirectAttributes redirectAttributes) {
if(user.getEmail().equals("abc") && user.getPassword().equals("123")){
return new ModelAndView("Dashboard", "name", model);
}
else
{
return new ModelAndView("Login", "name", model);
}
}
In this answer it's explained how to do it via java config or xml config.

excelViewResolver not working when using java based configuration with spring mvc 4

I have the requirement to display a document in excel format in my spring mvc application. I have added excelViewResolver to my AppConfig but unable to process the excel file. Please help.
Following is the AppConfig class
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = "com.testapps.myapps")
public class AppConfig extends WebMvcConfigurerAdapter {
private static final String autowire = null;
/*
* Configure ContentNegotiationManager
*/
#Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.ignoreAcceptHeader(true).defaultContentType(
MediaType.TEXT_HTML);
}
/*
* Configure ContentNegotiatingViewResolver
*/
#Bean
public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) {
ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
resolver.setContentNegotiationManager(manager);
// Define all possible view resolvers
List<ViewResolver> resolvers = new ArrayList<ViewResolver>();
resolvers.add(jspViewResolver());
resolvers.add(excelViewResolver());
resolver.setViewResolvers(resolvers);
return resolver;
}
/*
* Configure View resolver to provide XLS output using Apache POI library to
* generate XLS output for an object content
*/
#Bean(name="excelView")
public ViewResolver excelViewResolver() {
System.out.println("inside view resolver for excel");
return new ExcelViewResolver();
}
/*
* Configure View resolver to provide HTML output This is the default format
* in absence of any type suffix.
*/
#Bean
public ViewResolver jspViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
#Bean(name = "multipartResolver")
public StandardServletMultipartResolver resolver() {
return new StandardServletMultipartResolver();
}
}
WebAppInitializer.java
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { AppConfig.class };
}
#Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
#Override
protected String[] getServletMappings() {
return new String[] { "*.html" };
}
#Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
registration.setMultipartConfig(getMultipartConfigElement());
}
private MultipartConfigElement getMultipartConfigElement() {
MultipartConfigElement multipartConfigElement = new MultipartConfigElement( LOCATION, MAX_FILE_SIZE, MAX_REQUEST_SIZE, FILE_SIZE_THRESHOLD);
return multipartConfigElement;
}
private static final String LOCATION = "C:/TESTdevelopments/files/"; // Temporary location where files will be stored
private static final long MAX_FILE_SIZE = 5242880; // 5MB : Max file size.
// Beyond that size spring will throw exception.
private static final long MAX_REQUEST_SIZE = 20971520; // 20MB : Total request size containing Multi part.
private static final int FILE_SIZE_THRESHOLD = 0; // Size threshold after which files will be written to disk
}
ExcelBuilder.java
public class ExcelBuilder extends AbstractXlsxView {
private static final DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.SHORT);
#Override
protected void buildExcelDocument(Map<String, Object> model,
Workbook workbook,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
// change the file name
response.setHeader("Content-Disposition", "attachment; filename=\"my-xlsx-file.xlsx\"");
#SuppressWarnings("unchecked")
List<Course> courses = (List<Course>) model.get("courses");
// create excel xls sheet
Sheet sheet = workbook.createSheet("Spring MVC AbstractXlsxView");
// create header row
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("ID");
header.createCell(1).setCellValue("Name");
header.createCell(2).setCellValue("Date");
// Create data cells
int rowCount = 1;
for (Course course : courses){
Row courseRow = sheet.createRow(rowCount++);
courseRow.createCell(0).setCellValue(course.getId());
courseRow.createCell(1).setCellValue(course.getName());
courseRow.createCell(2).setCellValue(DATE_FORMAT.format(course.getDate()));
}
}
}
ExcelViewResolver.java
public class ExcelViewResolver implements ViewResolver{
#Override
public View resolveViewName(String viewName, Locale locale) throws Exception {
ExcelBuilder view = new ExcelBuilder();
return view;
}
}
and finally controller class mapping...
#RequestMapping(value = "/downloadExcel.html", method = RequestMethod.GET)
public ModelAndView downloadExcel() {
// create some sample data
List<courseRecordParams> reports = courseRegisterService.generateReportsGeneral();
// return a view which will be resolved by an excel view resolver
return new ModelAndView("excelView", "reports", reports);
}
The problem is I am not able to view the excel file. Its generating a file not found error as in JBWEB000124: The requested resource is not available. since it tries to open a nonexisting jsp.
Please help.
Thanks in advance.
Found a solution to this problem.
#Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer
.favorPathExtension(true)
.favorParameter(false)
.ignoreAcceptHeader(true)
.useJaf(false)
.defaultContentType(MediaType.APPLICATION_JSON);
}
Also, I removed the excelviewresolver which was unnecessary since this is already handled by multipartviewresolver.
After this the excel file was getting generated without any problem.

Can not work internationalization in spring mvc with thymeleaf

I have started a project with Spring MVC with thymeleaf. I want to that it is internationalize. I had copied official spring tutorial. But When I click tr button , words aren't changed. I am pasting my codes. I assumed that getLabel function of ResourceManager service is getting label current language. But I am not sure. How can I proceed?
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = {"com.yummynoodlebar.web.controller","com.yummynoodlebar.web.service","com.yummynoodlebar.web.domain"})
public class WebConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
#Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
registry.addInterceptor(localeChangeInterceptor);
}
#Bean
public LocaleResolver localeResolver() {
CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
cookieLocaleResolver.setDefaultLocale(StringUtils.parseLocaleString("en"));
return cookieLocaleResolver;
}
#Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html");
//NB, selecting HTML5 as the template mode.
resolver.setTemplateMode("HTML5");
resolver.setCharacterEncoding("UTF-8");
resolver.setCacheable(false);
return resolver;
}
#Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver());
return engine;
}
#Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setOrder(1);
viewResolver.setViewNames(new String[]{"*"});
viewResolver.setCache(false);
return viewResolver;
}
#Bean(name = "labelSource")
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasenames("classpath:i18n/labels");
// if true, the key of the message will be displayed if the key is not
// found, instead of throwing a NoSuchMessageException
messageSource.setUseCodeAsDefaultMessage(true);
messageSource.setDefaultEncoding("UTF-8");
// # -1 : never reload, 0 always reload
messageSource.setCacheSeconds(0);
return messageSource;
}
}
#Component
public class ResourceManager {
#Resource(name = "labelSource")
private MessageSource messageSource;
public String getLabel(String code) {
return messageSource.getMessage(code, null, Locale.getDefault());
}
}
I have solved my problem. I had changed getLabel function. New code is following
public String getLabel(String code) {
return messageSource.getMessage(code, null, LocaleContextHolder.getLocale());
}

Resources