Mar*_*era 19 java spring servlets spring-mvc
作为一个例子,取子域映射.
本文:在Google App Engine上为同一应用程序管理多个域和子域 建议解析Filter上的子域并将变量分配给ServletRequest头.
然后映射将如下所示:
@RequestMapping(value = "/path", headers="subdomain=www")
public String subsiteIndexPage(Model model,HttpServletRequest request) { ... }
Run Code Online (Sandbox Code Playgroud)
如果我们想创建自定义@RequestMapping属性,例如子域,例如.创建这样的映射:
@RequestMapping(value = "/some/action", subdomain = "www")
public String handlerFunction(){ ... }
Run Code Online (Sandbox Code Playgroud)
我们应该使用我们自己的实现覆盖@RequestMapping @interface定义并覆盖RequestMappingHandlerMapping受保护的方法
(如JIRA所述:" 允许自定义请求映射条件SPR-7812 ").
这样对吗?任何人都可以提供一个提示,如何实现这一功能?
想法1:
正如原始jira线程所建议的,是创建自己的实现RequestCondition
有一个项目在github上使用这个解决方案:https://github.com/rstoyanchev/spring-mvc-31-demo/
和相关的问题:在Spring mvc 3.1中添加自定义RequestCondition
也许像@Subdomain("www")Type和Method 这样的映射是可能的解决方案吗?
Mar*_*era 30
我已经根据引用的spring-mvc-31-demo创建了解决方案
此解决方案可用于仅映射现在的单个RequestCondition.我创建了两个要通知的问题,应该更改:
https://github.com/rstoyanchev/spring-mvc-31-demo/issues/5
https://jira.springsource.org/browse/SPR-9350
此解决方案使用Spring 3.1.1.RELEASE平台的自定义@RequestCondition功能
用法
例1:
@Controller
@SubdomainMapping(value = "subdomain", tld = ".mydomain.com")
class MyController1 {
// Code here will be executed only on address match:
// subdomain.mydomain.com
}
Run Code Online (Sandbox Code Playgroud)
例2:
@Controller
class MyController2 {
@RequestMapping("/index.html")
@SubdomainMapping("www")
public function index_www(Map<Object, String> map){
// on www.domain.com
// where ".domain.com" is defined in SubdomainMapping.java
}
@RequestMapping("/index.html")
@SubdomainMapping("custom")
public function index_custom(Map<Object, String> map){
// on custom.domain.com
// where ".domain.com" is defined in SubdomainMapping.java
}
}
Run Code Online (Sandbox Code Playgroud)
我们需要三个文件
SubdomainMapping.java
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface SubdomainMapping {
/**
* This param defines single or multiple subdomain
* Where the Method/Type is valid to be called
*/
String[] value() default {};
/**
* This param defines site domain and tld
* It's important to put the leading dot
* Not an array, so cannot be used for mapping multiple domains/tld
*/
String tld() default ".custom.tld";
}
Run Code Online (Sandbox Code Playgroud)
SubdomainRequestCondition.java
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.servlet.mvc.condition.RequestCondition;
public class SubdomainRequestCondition implements
RequestCondition<SubdomainRequestCondition> {
private final Set<String> subdomains;
private final String tld;
public SubdomainRequestCondition(String tld, String... subdomains) {
this(tld, Arrays.asList(subdomains));
}
public SubdomainRequestCondition(String tld, Collection<String> subdomains) {
this.subdomains = Collections.unmodifiableSet(new HashSet<String>(
subdomains));
this.tld = tld;
}
@Override
public SubdomainRequestCondition combine(SubdomainRequestCondition other) {
Set<String> allRoles = new LinkedHashSet<String>(this.subdomains);
allRoles.addAll(other.subdomains);
return new SubdomainRequestCondition(tld, allRoles);
}
@Override
public SubdomainRequestCondition getMatchingCondition(
HttpServletRequest request) {
try {
URL uri = new URL(request.getRequestURL().toString());
String[] parts = uri.getHost().split(this.tld);
if (parts.length == 1) {
for (String s : this.subdomains) {
if (s.equalsIgnoreCase(parts[0])) {
return this;
}
}
}
} catch (Exception e) {
e.printStackTrace(System.err);
}
return null;
}
@Override
public int compareTo(SubdomainRequestCondition other,
HttpServletRequest request) {
return org.apache.commons.collections.CollectionUtils.removeAll(other.subdomains, this.subdomains).size();
}
}
Run Code Online (Sandbox Code Playgroud)
SubdomainRequestMappingHandlerMapping.java
import java.lang.reflect.Method;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.servlet.mvc.condition.RequestCondition;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
public class CustomRequestMappingHandlerMapping extends
RequestMappingHandlerMapping {
@Override
protected RequestCondition<?> getCustomTypeCondition(Class<?> handlerType) {
SubdomainMapping typeAnnotation = AnnotationUtils.findAnnotation(
handlerType, SubdomainMapping.class);
return createCondition(typeAnnotation);
}
@Override
protected RequestCondition<?> getCustomMethodCondition(Method method) {
SubdomainMapping methodAnnotation = AnnotationUtils.findAnnotation(
method, SubdomainMapping.class);
return createCondition(methodAnnotation);
}
private RequestCondition<?> createCondition(SubdomainMapping accessMapping) {
return (accessMapping != null) ? new SubdomainRequestCondition(
accessMapping.tld(), accessMapping.value()) : null;
}
}
Run Code Online (Sandbox Code Playgroud)
安装目录
重要提示:到目前为止,无法将此解决方案与XML元素
<mvc:annotation-driven />一起使用,请参阅JIRA https://jira.springsource.org/browse/SPR-9344以获取解释
SubdomainRequestMappingHandlerMapping类RequestMappingHandlerMappingRequestMappingHandlerMapping(可能在订单= 0)有关实现此解决方案的更广泛说明,请参阅相关的github项目
| 归档时间: |
|
| 查看次数: |
21291 次 |
| 最近记录: |