Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ca6bbe1

Browse files
Spring MVC ThemeResolver
Aplicar y cambiar el tema (estilo y diseño) de una aplicación web.
1 parent e175905 commit ca6bbe1

File tree

11 files changed

+196
-0
lines changed

11 files changed

+196
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project-shared-configuration>
3+
<!--
4+
This file contains additional configuration written by modules in the NetBeans IDE.
5+
The configuration is intended to be shared among all the users of project and
6+
therefore it is assumed to be part of version control checkout.
7+
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
8+
-->
9+
<spring-data xmlns="http://www.netbeans.org/ns/spring-data/1">
10+
<config-files>
11+
<config-file>src/main/webapp/WEB-INF/springmvc-servlet.xml</config-file>
12+
</config-files>
13+
<config-file-groups/>
14+
</spring-data>
15+
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
16+
<!--
17+
Properties that influence various parts of the IDE, especially code formatting and the like.
18+
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
19+
That way multiple projects can share the same settings (useful for formatting rules for example).
20+
Any value defined here will override the pom.xml file value but is only applicable to the current project.
21+
-->
22+
<org-netbeans-modules-javascript2-requirejs.enabled>true</org-netbeans-modules-javascript2-requirejs.enabled>
23+
</properties>
24+
</project-shared-configuration>

‎tutorial_webmvc_theme/pom.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>carmelo.spring</groupId>
5+
<artifactId>tutorial_webmvc_theme</artifactId>
6+
<packaging>war</packaging>
7+
<version>1.0</version>
8+
<name>tutorial_webmvc_theme</name>
9+
10+
<properties>
11+
<spring.version>4.3.7.RELEASE</spring.version>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<maven.compiler.source>1.8</maven.compiler.source>
14+
<maven.compiler.target>1.8</maven.compiler.target>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.springframework</groupId>
20+
<artifactId>spring-webmvc</artifactId>
21+
<version>${spring.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>javax.servlet</groupId>
25+
<artifactId>javax.servlet-api</artifactId>
26+
<version>3.1.0</version>
27+
<scope>provided</scope>
28+
</dependency>
29+
<dependency>
30+
<groupId>javax.servlet</groupId>
31+
<artifactId>jstl</artifactId>
32+
<version>1.2</version>
33+
</dependency>
34+
</dependencies>
35+
36+
<build>
37+
<finalName>tutorial_webmvc_theme</finalName>
38+
<plugins>
39+
<plugin>
40+
<groupId>org.apache.maven.plugins</groupId>
41+
<artifactId>maven-war-plugin</artifactId>
42+
<version>2.3</version>
43+
<configuration>
44+
<failOnMissingWebXml>false</failOnMissingWebXml>
45+
</configuration>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
50+
</project>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package carmelo.spring.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.ui.context.support.ResourceBundleThemeSource;
7+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
8+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
9+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
10+
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
11+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
12+
import org.springframework.web.servlet.theme.CookieThemeResolver;
13+
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
14+
15+
@EnableWebMvc
16+
@Configuration
17+
@ComponentScan(basePackages = {"carmelo.spring.controller"})
18+
public class WebAppConfig extends WebMvcConfigurerAdapter {
19+
20+
@Bean
21+
public ResourceBundleThemeSource themeSource() {
22+
ResourceBundleThemeSource themeSource = new ResourceBundleThemeSource();
23+
themeSource.setBasenamePrefix("themes.tema-");
24+
return themeSource;
25+
}
26+
27+
@Bean
28+
public CookieThemeResolver themeResolver() {
29+
CookieThemeResolver resolver = new CookieThemeResolver();
30+
resolver.setDefaultThemeName("oscuro");
31+
resolver.setCookieName("cookie-theme");
32+
return resolver;
33+
}
34+
35+
@Bean
36+
public ThemeChangeInterceptor themeChangeInterceptor() {
37+
ThemeChangeInterceptor interceptor = new ThemeChangeInterceptor();
38+
interceptor.setParamName("mytheme");
39+
return interceptor;
40+
}
41+
42+
@Override
43+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
44+
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
45+
}
46+
47+
@Override
48+
public void addInterceptors(InterceptorRegistry registry) {
49+
registry.addInterceptor(themeChangeInterceptor());
50+
}
51+
52+
@Override
53+
public void configureViewResolvers(ViewResolverRegistry registry) {
54+
registry.jsp("/WEB-INF/views/", ".jsp");
55+
}
56+
57+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package carmelo.spring.config;
2+
3+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
4+
5+
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
6+
7+
@Override
8+
protected Class<?>[] getRootConfigClasses() {
9+
return null;
10+
}
11+
12+
@Override
13+
protected Class<?>[] getServletConfigClasses() {
14+
return new Class[]{WebAppConfig.class};
15+
}
16+
17+
@Override
18+
protected String[] getServletMappings() {
19+
return new String[]{"/"};
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package carmelo.spring.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.servlet.ModelAndView;
6+
7+
@Controller
8+
public class ThemeController {
9+
10+
@RequestMapping("/")
11+
public ModelAndView homePage() {
12+
return new ModelAndView("home");
13+
}
14+
15+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
stylesheet=css/claro.css
2+
name=THEME-CLARO
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
stylesheet=css/oscuro.css
2+
name=THEME-OSCURO
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Context antiJARLocking="true" path="/springtheme"/>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
2+
3+
<html>
4+
<head>
5+
<link rel="stylesheet" href="<spring:theme code='stylesheet'/>" type="text/css" />
6+
<title>Spring MVC ThemeResolver</title>
7+
</head>
8+
<body>
9+
10+
<h1>Spring MVC ThemeResolver</h1>
11+
<h3>Tema actual: <spring:theme code='name'/> </h3>
12+
aplicar tema: <a href="?mytheme=claro">claro</a> | <a href="?mytheme=oscuro">oscuro</a>
13+
14+
</body>
15+
</html>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body {
2+
color: darkslategray;
3+
background-color: whitesmoke;
4+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /