我正在尝试实现类似于GitHub使用的"上下文"系统.例如,可以创建属于用户或用户所属的公司之一的帖子,这取决于用户是在"用户"上下文中还是在引用其中一个公司的上下文中.
作为其中的一部分,我希望能够根据用户的当前上下文进行路由.例如,如果用户在他们自己的上下文中,/dashboard应该路由到users/show,但如果他们在ID为35的公司的上下文中,则/dashboard应该路由到companies/35/dashboard.
我可以路由/dashboard到一个负责做出这样决定的特殊控制器,比如context#dashboard可以做一个redirect_to,但这感觉不太正确(也许是因为我们正在采用Rails路由模块负责的逻辑并将其移动到控制器?)
在Rails 3中解决这个问题的正确方法是什么?
我正在创建一个用于处理邮件发送的类.对于其中一些邮件,我需要构建链接(帐户激活,重置密码,确认等).
我不想硬编码链接的主机,因为dev,test和prod的主机不同.所以我需要在我的邮件类中以编程方式获取主机名和应用程序上下文.
我们在这个项目中使用Spring MVC.
我用一个例子说明我想要的东西.
在我们的JSP中,我们有这样的代码片段:
<script src="<%=request.getContextPath()%>/js/jquery-1.7.js" type="text/javascript">
Run Code Online (Sandbox Code Playgroud)
在此上下文中,request.getContextPath()将解析为
http:// localhost:8080/applicationName
这就是我想要的邮件课程.
我试图在我的班级中实现ServletContextAware,以为我能够使用servletContext.getContextPath()类似的结果.但是这个语句只解析为/ applicationName
所以我的问题是:如何在我的邮件类中获得完整的主机和上下文(http:// localhost:8080/applicationName)?我可以使用除ServletContextAware之外的其他接口吗?请帮忙 :-)
关心丹尼尔
How does Tomcat 8.0 serve http requests in the following scenario?
Let's say we have deployed the two web applications "ROOT.war" and "Foo.war" on a server with the name "www.host.com". Furthermore, let's assume that ROOT.war contains a subfolder named "Foo" which contains a file "mypage.html". Additonally, let's assume "Foo.war" also contains a file named "mypage.html". So after extraction of the war-files Tomcat's webapps directory should look like this:
webapps\ROOT\Foo\mypage.html
webapps\Foo\mypage.html
Run Code Online (Sandbox Code Playgroud)
If a user made a request to
http://www.host.com/Foo/mypage.html
Run Code Online (Sandbox Code Playgroud)
in his …
我的Tomcat/webapps目录中有一个webapp.我的应用程序目录是"site",显示为http:localhost:8080/site
我想让网站显示为http:// localhost:8080 /
我已经阅读了文档并尝试在我的Tomcat/conf/Catalina/localhost目录中创建ROOT.xml文件来创建ROOT上下文路径但是没有建立连接.
有人可以朝着正确的方向推动我吗?这是ROOT.xml文件的内容.
<Context docBase="site" path="/"> </Context>
Run Code Online (Sandbox Code Playgroud) 我正在使用JSF和XHTML模板,我在模板中使用CSS文件,背景图像被称为:
background-image: url("../../images/imageFile.jpg");
Run Code Online (Sandbox Code Playgroud)
因为我正在使用模板,我发现我必须为页面和样式/图像保持相同的深度才能正确应用样式页面,但项目有变化,现在它需要文件夹和页面的可变深度,使这种方法不再可行.
然后我的问题是:
是否有某种方式来代替相对路径(../../, ../由上下文路径(等)<%Request.getContextPath()%>, #{facesContext.requestContextPath}等等),CSS文件里面?
----- -----更新
绝对路径是不可能的.我需要基于模板的页面(无论它们的深度)能够找到我的CSS文件引用的样式和图像资源.
目前只有当页面,样式和图像在应用程序的文件夹结构中共享相同的深度时才有可能,但是我不能再保留这种方法,因为新的项目要求阻止我这样做.
我的项目文件结构的示例,作为<root>应用程序根目录的路径:
CSS(depth-2):<root>/styles/global/myStyles.css
包含具有深度2路径引用的样式,例如:
background-image: url("../../images/imageFile.jpg");
Run Code Online (Sandbox Code Playgroud)
图像(深度-2):<root>/images/basic/imageFile.jpg
模板(深度-2):<root>/template/general/template1.xhtml
页面(深度-2):( <root>/pages/folder1/page1.xhtml 工作正常)
页面(深度-N):( <root>/pages/folder1/.../folderN/page2.xhtml 图像和样式损坏)
我有一个简单的网络应用程序
webapp
static
images
- a.gif
pages
- test.html
WEB-INF
pages
- test.jsp
Run Code Online (Sandbox Code Playgroud)
在test.html中,有
<img src="/static/images/a.gif"/>
Run Code Online (Sandbox Code Playgroud)
问题是,在我将uri更改为之前,图像不会显示
<img src="/web app name/static/images/a.gif"/>
Run Code Online (Sandbox Code Playgroud)
但是我在URI上加载test.html
http://server/web app name/static/pages/test.html
Run Code Online (Sandbox Code Playgroud)
我在web.xml中配置了静态资源映射,如下所示.
<servlet>
<servlet-name>springWeb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>resourceServlet</servlet-name>
<servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>resourceServlet</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?我确实希望在DEV阶段将这些静态资源保留在应用程序中,而不是将它们移动到HTTP服务器.
非常感谢.
我想在src属性中使用绝对路径,同时在JSP中包含Javascript和CSS文件.
使用相对URL不符合我的目的,类似于下面的那个:
<script type="text/javascript" src="../js/jquery-1.6.4.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
如果我使用绝对URL,
<script type="text/javascript" src="/appName/js/jquery-1.6.4.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
然后我硬编码不适合的Web应用程序名称...
我找到的最佳选择是:
<base href="${pageContext.request.contextPath}" />
Run Code Online (Sandbox Code Playgroud)
但是,它只适用于href属性 <a href="home.jsp" />
我尝试使用以下代码片段为spring rest mocks设置上下文路径:
private MockMvc mockMvc;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation))
.alwaysDo(document("{method-name}/{step}/",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint())))
.build();
}
@Test
public void index() throws Exception {
this.mockMvc.perform(get("/").contextPath("/api").accept(MediaTypes.HAL_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("_links.business-cases", is(notNullValue())));
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
java.lang.IllegalArgumentException: requestURI [/] does not start with contextPath [/api]
Run Code Online (Sandbox Code Playgroud)
怎么了? 是否可以在代码中的单个位置指定contextPath,例如直接在构建器中?
在这里控制器
@RestController
@RequestMapping(value = "/business-case", produces = MediaType.APPLICATION_JSON_VALUE)
public class BusinessCaseController {
private static final Logger LOG = LoggerFactory.getLogger(BusinessCaseController.class);
private final BusinessCaseService businessCaseService;
@Autowired
public BusinessCaseController(BusinessCaseService businessCaseService) {
this.businessCaseService = businessCaseService;
}
@Transactional(rollbackFor = Throwable.class, …Run Code Online (Sandbox Code Playgroud) 我正在使用IBM 的 IHS Webserver,它构建在 Apache Web 服务器版本 2.2.4 之上。
我的要求是在匹配中使用正则表达式代理传递各种上下文路径。
我尝试使用,ProxyPassMatch但出现以下错误
错误:“无效的 ProxyPass|ProxyPassMatch 参数。参数必须采用‘key=value’形式”
<LocationMatch "^/(ae/en|ar/en|ar/es|at/en|au/en|be/en|br/en)/">
Order Allow,Deny
Allow from all
ProxyPass http://www.xyz.com.au:80/au/en/ #(should keep varying as per the regex matched in location match )
ProxyReverse http://www.xyz.com.au:80/au/en/ #(should keep varying as per the regex matched in location match )
</LocationMatch>
Run Code Online (Sandbox Code Playgroud)
请建议如何实现这一点。
问候斯里达尔
我有一个 springboot 应用程序,我想将其部署在 Kubernetes 上(我使用的是 minikube),并使用从环境变量中获取的自定义上下文路径。
我编译了一个 app.war 文件。在Linux中导出环境变量如下:
export SERVER_SERVLET_CONTEXT_PATH=/app
然后在我的机器上启动我的应用程序,如下所示:
java -jar app.war --server.servlet.context-path=$(printenv CONTEXT_PATH)
它按预期工作,我可以使用 url localhost:8080/app/访问我的应用程序抛出浏览器
我想在 minikube 上实现同样的目标,所以我准备了这些配置文件:
Dockerfile:
FROM openjdk:8
ADD app.war app.war
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.war", "--server.servlet.context-path=$(printenv CONTEXT_PATH)"]
Run Code Online (Sandbox Code Playgroud)部署配置文件:
apiVersion: apps/v1
kind: Deployment
metadata:
name: esse-deployment-1
labels:
app: esse-1
spec:
replicas: 1
selector:
matchLabels:
app: esse-1
template:
metadata:
labels:
app: esse-1
spec:
containers:
- image: mysql:5.7
name: esse-datasource
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: root
- image: esse-application …Run Code Online (Sandbox Code Playgroud)environment-variables contextpath spring-boot kubernetes minikube
contextpath ×10
java ×3
spring-mvc ×2
tomcat ×2
apache ×1
css ×1
deployment ×1
jsf ×1
jsp ×1
kubernetes ×1
minikube ×1
overlapping ×1
regex ×1
root ×1
routing ×1
servlets ×1
shadowing ×1
spring ×1
spring-boot ×1
templates ×1
url ×1