无法弄清楚如何创建这个if语句(Cannot figure out how to create this if statement)
这是世界上最简单的php如果声明曾经创建,我怎么也想不出怎么做,在一个。 基本上我有一个难倒的时刻,需要社区的帮助。
这是我的功能:
protected function _traverse_options($name, $type = ''){ if(isset($this->_options[$name][$type])){ echo $this->_options[$name][$type]; } }
我需要的if语句是检查三件事:
- 如果type不为null 但类型不是'之前'
- 如果type不为null 但类型不是'after'
我尝试过:
if($type != '' && $type != 'before' || $type != '' && $type != 'after'){}
这怎么都行不通。
我知道这很简单,但我想不出来? 应该
||
是&&
??This is the worlds easiest php if statement ever created, how ever ever I cannot figure out how to do it, in one. Essentially I am having a stumped moment and require the communities help .
This is my function:
protected function _traverse_options($name, $type = ''){ if(isset($this->_options[$name][$type])){ echo $this->_options[$name][$type]; } }
The if statement I need is to check for three things:
- If type is not null but type is not 'before'
- if type is not null but type is not 'after'
I tried doing:
if($type != '' && $type != 'before' || $type != '' && $type != 'after'){}
How ever that doesn't work.
I know this is simple, but I cannot figure it out? should
||
be&&
??
原文:https://stackoverflow.com/questions/15669147
最满意答案
视图解析器的合约指定视图解析器可以返回null以指示无法找到视图。 但是,并非所有视图解析器都这样做,因为在某些情况下,解析器根本无法检测视图是否存在。 例如,InternalResourceViewResolver在内部使用RequestDispatcher,并且调度是确定JSP是否存在的唯一方法,但此操作只能执行一次。 VelocityViewResolver和其他一些同样适用。 检查特定视图解析程序的javadoc,以查看它是否报告不存在的视图。 因此,将一个InternalResourceViewResolver放在链中的最后一个位置会导致链未被完全检查,因为InternalResourceViewResolver将始终返回一个视图!
重定向到视图
如前所述,控制器通常返回逻辑视图名称,视图解析器将其解析为特定的视图技术。 对于通过Servlet或JSP引擎处理的JSP等视图技术,此解决方案通常通过InternalResourceViewResolver和InternalResourceView的组合来处理,InternalResolver和InternalResourceView通过Servlet API的RequestDispatcher.forward(..)方法或RequestDispatcher发出内部转发或包含。 .include()方法。 对于其他视图技术,例如Velocity,XSLT等,视图本身将内容直接写入响应流。
RedirectView的
作为控制器响应的结果,强制重定向的一种方法是控制器创建并返回Spring的RedirectView实例。 在这种情况下,DispatcherServlet不使用普通的视图解析机制。 而是因为它已经被赋予(重定向)视图,DispatcherServlet只是指示视图执行其工作。 RedirectView依次调用HttpServletResponse.sendRedirect()将HTTP重定向发送到客户端浏览器。
如果您使用RedirectView并且视图是由控制器本身创建的,则建议您将重定向URL配置为注入控制器,以便它不会被烘焙到控制器中,而是在上下文中与视图名称一起配置。 名为“重定向:前缀”的部分有助于此解耦。
重定向:前缀
虽然RedirectView的使用工作正常,但如果控制器本身创建RedirectView,则无法避免控制器意识到重定向正在发生的事实。 这实际上并不是最理想的,而且会把事情过于紧密。 控制器不应该真正关心如何处理响应。 通常,它应该仅根据已注入其中的视图名称进行操作。
特殊的重定向:前缀允许您完成此操作。 如果返回的视图名称具有前缀redirect:,则UrlBasedViewResolver(以及所有子类)会将此识别为需要重定向的特殊指示。 视图名称的其余部分将被视为重定向URL。
净效果与控制器返回RedirectView的效果相同,但现在控制器本身可以简单地按照逻辑视图名称进行操作。 逻辑视图名称(例如redirect:/ myapp / some / resource)将相对于当前Servlet上下文重定向,而重定向等名称: http : //myhost.com/some/arbitrary/path将重定向到绝对URL。
转发:前缀
对于最终由UrlBasedViewResolver和子类解析的视图名称,也可以使用特殊的forward:前缀。 这会创建一个InternalResourceView(最终会对视图名称的其余部分执行RequestDispatcher.forward()),该视图名称被视为URL。 因此,此前缀对于InternalResourceViewResolver和InternalResourceView(例如,对于JSP)没有用。 但是,当您主要使用其他视图技术时,前缀可能会有所帮助,但仍希望强制Servlet / JSP引擎处理资源的转发。 (请注意,您也可以链接多个视图解析器。)
与redirect:前缀一样,如果将带有forward:前缀的视图名称注入控制器,则控制器不会检测到在处理响应方面发生了什么特殊情况。
DOCS: https : //docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html
The contract of a view resolver specifies that a view resolver can return null to indicate the view could not be found. Not all view resolvers do this, however, because in some cases, the resolver simply cannot detect whether or not the view exists. For example, the InternalResourceViewResolver uses the RequestDispatcher internally, and dispatching is the only way to figure out if a JSP exists, but this action can only execute once. The same holds for the VelocityViewResolver and some others. Check the javadocs of the specific view resolver to see whether it reports non-existing views. Thus, putting an InternalResourceViewResolver in the chain in a place other than the last results in the chain not being fully inspected, because the InternalResourceViewResolver will always return a view!
Redirecting to Views
As mentioned previously, a controller typically returns a logical view name, which a view resolver resolves to a particular view technology. For view technologies such as JSPs that are processed through the Servlet or JSP engine, this resolution is usually handled through the combination of InternalResourceViewResolver and InternalResourceView, which issues an internal forward or include via the Servlet API’s RequestDispatcher.forward(..) method or RequestDispatcher.include() method. For other view technologies, such as Velocity, XSLT, and so on, the view itself writes the content directly to the response stream.
RedirectView
One way to force a redirect as the result of a controller response is for the controller to create and return an instance of Spring’s RedirectView. In this case, DispatcherServlet does not use the normal view resolution mechanism. Rather because it has been given the (redirect) view already, the DispatcherServlet simply instructs the view to do its work. The RedirectView in turn calls HttpServletResponse.sendRedirect() to send an HTTP redirect to the client browser.
If you use RedirectView and the view is created by the controller itself, it is recommended that you configure the redirect URL to be injected into the controller so that it is not baked into the controller but configured in the context along with the view names. The the section called “The redirect: prefix” facilitates this decoupling.
The redirect: prefix
While the use of RedirectView works fine, if the controller itself creates the RedirectView, there is no avoiding the fact that the controller is aware that a redirection is happening. This is really suboptimal and couples things too tightly. The controller should not really care about how the response gets handled. In general it should operate only in terms of view names that have been injected into it.
The special redirect: prefix allows you to accomplish this. If a view name is returned that has the prefix redirect:, the UrlBasedViewResolver (and all subclasses) will recognize this as a special indication that a redirect is needed. The rest of the view name will be treated as the redirect URL.
The net effect is the same as if the controller had returned a RedirectView, but now the controller itself can simply operate in terms of logical view names. A logical view name such as redirect:/myapp/some/resource will redirect relative to the current Servlet context, while a name such as redirect:http://myhost.com/some/arbitrary/path will redirect to an absolute URL.
The forward: prefix
It is also possible to use a special forward: prefix for view names that are ultimately resolved by UrlBasedViewResolver and subclasses. This creates an InternalResourceView (which ultimately does a RequestDispatcher.forward()) around the rest of the view name, which is considered a URL. Therefore, this prefix is not useful with InternalResourceViewResolver and InternalResourceView (for JSPs for example). But the prefix can be helpful when you are primarily using another view technology, but still want to force a forward of a resource to be handled by the Servlet/JSP engine. (Note that you may also chain multiple view resolvers, instead.)
As with the redirect: prefix, if the view name with the forward: prefix is injected into the controller, the controller does not detect that anything special is happening in terms of handling the response.
DOCS: https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html
相关问答
更多-
是的,您可以使用通配符: http : //emberjs.com/guides/routing/defining-your-routes/#toc_wildcard-globbing-routes 从'余烬'进口Ember; 从'./config/environment'导入配置; var Router = Ember.Router.extend({location:config.locationType}); Router.map(function() { this.resource('index' ...
-
在TEMPLATES设置中,尝试将DIRS设置更改为: 'DIRS': [os.path.join(BASE_DIR, 'templates')], 这是通常的方法,我看MAIN_DIR你目前使用MAIN_DIR任何理由。 保持模板为 template_name = "base/index.html" In your TEMPLATES setting, try changing your DIRS setting to: 'DIRS': [os.path.join(BASE_DIR, 'templat ...
-
如果URL路径为空,请添加条件以提供index.html: path := "public" if req.URL.Path == "/" { path += "/index.html" } else { path += req.URL.Path } 此外,使用net/http.ServeFile不是手动将数据写入输出流是个好主意(请参阅net/http#ServeContent的文档以了解为什么这是一个好主意)。 值得注意的是,存在用于提供文件的内置处理程序 。 Add a condit ...
-
由于angularjs 1.1.5添加了这个问题,因此有几个错误报告: https://github.com/angular/angular.js/issues/2898 https://github.com/angular/angular.js/issues/2898 和别的 ... 这个错误在版本1.2.1中修复,可以下载。 变更集: https : //github.com/angular/angular.js/commit/d4d34aba6efbd98050235f5b264899bb788117 ...
-
您从不使用分页对象,而是创建了一个名为all_songs的单独上下文变量。 只需使用正确的上下文数据即可 {% for song in all_songs %} 应该 {% for song in home_list %} 您可能还想为其他查询集应用分页,尽管它可能会让多个列表产生混淆 You never use your paginated objects, instead you've made a separate context variable called all_songs. Simply ...
-
使用index.html作为/folder/的默认文件, 即。 将浏览器指向/folder/将您带到/folder/index.html 是一个如此广泛的惯例,你几乎肯定更好,不要搞乱它。 我的建议是: 将/index.html重命名为/old-part/index.html 将/index2.html重命名为/new-part/index.html 将/start.html重命名为/index.html Using index.html as the default file for /folder/, ...
-
在location块内使用rewrite ... last只需将/index.html重写到/index.html 。 你应该使用rewrite ... break 。 请参阅此文档了解详细信息 如果您的应用程序还需要资源(css,js,images),您可以采用另一种方法返回静态文件(如果存在),例如: server { listen 80; root C:/Users/Sergey/Desktop/xxx/angular2do; location / { ...
-
视图解析器的合约指定视图解析器可以返回null以指示无法找到视图。 但是,并非所有视图解析器都这样做,因为在某些情况下,解析器根本无法检测视图是否存在。 例如,InternalResourceViewResolver在内部使用RequestDispatcher,并且调度是确定JSP是否存在的唯一方法,但此操作只能执行一次。 VelocityViewResolver和其他一些同样适用。 检查特定视图解析程序的javadoc,以查看它是否报告不存在的视图。 因此,将一个InternalResourceViewR ...
-
为避免双重重定向,在.htaccess文件中有另一个符合这两个条件的规则: Options +FollowSymlinks -MultiViews RewriteEngine on RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteCond %{REQUEST_URI} ^(.*/)index\.html$ [NC] RewriteRule . http://www.%{HTTP_HOST}%1 [R=301,NE,L] RewriteCond %{HTTP_HO ...
-
您使用的是哪个版本的Django? 你有没有设置TEMPLATE_DIRS或DIRS来指示Django在哪里寻找模板? 发布settings.py,urls.py和views.py将有助于首先解决问题。 查看最新版本模板设置的文档 。 更新 从settings.py中删除TEMPLATE_DIRS 添加标准的Django 1.8.x模板设置 TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTempla ...