How to access subdir within images folder (getting 404)? - spring-mvc

I've added a new subdir within my images folder and cannot get the new images to resolve.
Failed to load resource: ... 404 (Not Found)
http://localhost:8080/mywebapp/content/images/subdir/mysubdirimage.png
My directory structure:
src
-- main
--java
--webapp
--content
--images // <- these resolve
--subdir // <- new subdir...resolve fail for images
I have tried adding the following but does't work:
<mvc:resources mapping="/content/**" location="/content/" />
mvc-dispatcher-servelet.xml:
<mvc:annotation-driven/>
<mvc:default-servlet-handler />
<mvc:resources mapping="/content/**" location="/content/" /> //<-- Added this..no go!
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"><value>/WEB-INF/views/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>

You are halfway there with the mvc-dispatcher-servlet line you added, but you need to change it to:
<mvc:resources mapping="/images/**" location="/content/images/" />
Also try changing the method in your controller where you are attempting to access the images to something like:
#RequestMapping(value = "/staticImages", method = RequestMethod.GET)
public String showImage() {
return "/images/subdir/mysubdirimage.png";
}
And finally, with the example above try the URL (as you were doing above):
http://localhost:8080/mywebapp/images/subdir/mysubdirimage.jpg
You should also be able to access the images through the #RequestMapping pattern defined in your controller. For example, using the example I gave you above, you would enter the URL:
http://localhost:8080/mywebapp/staticImages

Related

Share config custom.xml for specific site in Alfresco

I don't want few aspects to be visible in manage aspect for a particular site. So I changed share config custom XML. But this change is being reflected for all sites. How can I make this specific for a site?
Any help is appreciated.
Thanks in advance!!!
You should use the concept of seperate share modules per site, using a site evaluator:
create a share-extension-mysitename-module.xml file in alfresco\site-data\extensions\ that looks like so:
<id>My site module</id>
<auto-deploy>true</auto-deploy>
<evaluator type="site.module.evaluator">
<params>
<sites>mysitename</sites>
<applyForNonSites>false</applyForNonSites>
</params>
</evaluator>
<customizations>
<customization>
<targetPackageRoot>org.alfresco</targetPackageRoot>
<sourcePackageRoot>com.mypackage</sourcePackageRoot>
</customization>
</customizations>
<configurations>
<config evaluator="string-compare" condition="DocumentLibrary" >
...
<aspects>
<visible>
<aspect name="my:visibleaspect" />
</visible>
</aspects>
</config>
</configurations>
</module>
</modules>
As far as I know, I don't think this is possible to have a custom share-config by site.
I see two (probably unsatisfying) solutions :
You can create your custom evaluator and use it to make some parts accessibles (or not).
<bean id="evaluator.doclib.action.siteBased" class="xx.xx.xx.web.evaluator.SiteBasedEvaluator">
<property name="sites">
<list>
<value>mysite</value>
</list>
</property>
</bean>
public class SiteBasedEvaluator extends BaseEvaluator {
private List<String> sites;
public SiteBasedEvaluator() {
super();
}
public SiteBasedEvaluator(String... pSites) {
super();
sites = Arrays.asList(pSites);
}
public boolean evaluate(JSONObject jsonObject) {
Boolean isFound = false;
if (sites != null) {
for (String site : sites) {
isFound = site.equals(getSiteId(jsonObject));
if (isFound) {
break;
}
}
}
return isFound;
}
You can deploy two share war in your tomcat, each one having the share-config-custom.xml you want

Spring Framework - How to change locale in controller

I am new to Spring, and currently confusing about localization.
I'm using the following code to get text from messages_jp.properties file.
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
</bean>
And now I want to switch to text from messages_en.properties file, is there any ways to change locale in controller within if...else... block, not with using url params like "?lang=en", something like:
if (user.getLang() == 1) {
// set locale to en
} else {
// set locale to jp
}
Thanks in advance!
Try this one
<util:properties id="yourFileNameId" location="classpath:/yourFileName.properties"/>
In Controller
#Value("#{yourFileNameId['message_id']?:1}")
private int smalltext;

InternalResourceViewResolver to resolve both JSP and HTML together

I want org.springframework.web.servlet.view.InternalResourceViewResolver to resolve both JSP and HTML pages.
Is that possible?
You can configure an InternalResourceViewResolver something like this:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=""/>
</bean>
Where the WEB-INF/pages folder can contain both jsp and html pages and the suffix property is left empty.
Then in your controller, you can have methods that return html views and methods that return jsp views based on the suffix. For example, if index.html and index.jsp both exist in WEB-INF/pages you can do:
#RequestMapping("/htmlView")
public String renderHtmlView() {
return "index.html";
}
#RequestMapping("/jspView")
public String renderJspView() {
return "index.jsp";
}
However, as html pages are static and require no processing, you'd be better to use the <mvc:resources> tag rather than a view resolver for this type of page. See the docs for more info.

Spring MVC 3, Interceptor on all excluding some defined paths

Is it possible to apply an interceptor to all controllers and actions, except some that are defined?
Just to be clear, I am not interested in applying an interceptor on a list of defined ones. I want to define those to exclude.
Thanks!
Since Spring 3.2 they added that feature with the tag
mvc:exclude-mapping
See this example from the Spring documentation:
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/admin/**"/>
<bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/secure/*"/>
<bean class="org.example.SecurityInterceptor" />
</mvc:interceptor>
Here's the link to the doc
For java based configuration, from the docs
#Configuration
#EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
#Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LocaleInterceptor());
registry.addInterceptor(new ThemeInterceptor()).addPathPatterns("/**").excludePathPatterns("/admin/**");
registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/secure/*");
}
}
When configuring an interceptor, you can specify a path pattern. The interceptor will be invoked only for controllers which the path matches the interceptor path pattern.
ref: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-config-interceptor
But as you probably noticed it, the path pattern doesn't support exclusion.
So I think the only way is to code a blacklist of paths inside the interceptor. When the interceptor is invoked, retrieve the HttpServletRequest.getRequestURI() and check if the path is blacklisted or not.
You can build the blacklist inside a #PostConstruct annotated method of the interceptor, and so get the blacklisted path from a property file for instance.

Default formView for SimpleFormController?

Firstly I would like to say that I am quite new to Spring (in particular the MVC framework), and just trying to understand how everything works so please go easy on me.
I'm playing around with a dummy application that I've created, and I've created a simple login form that users can access via the /login.html bean. The bean definition is as follows:
<bean name="/login.html" class="test.controller.LoginController">
<property name="successView" value="list_messages.html" />
<property name="commandClass" value="test.domain.Login" />
<property name="commandName" value="login" />
</bean>
(the Login class is a simple object containing a username and password field with appropriate getters and setters).
The LoginController class does virtually nothing for now:
public class LoginController extends SimpleFormController
{
#Override
protected ModelAndView onSubmit(Object command, BindException errors) throws Exception
{
return new ModelAndView(new RedirectView(getSuccessView()));
}
}
Now I have one view resolver in my bean definition file, which goes as follows:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
To support my Login form I have a login.jsp file in my jsp directory.
My question is as follows: why does accessing /login.html redirect me to login.jsp? I have not specified a formView property for my form, so how does the view resolver know to redirect me to login.jsp?
Thanks in advance for any help!
Joseph.
When you do not specify The logical view name, Spring relies on DefaultRequestToViewNameTranslator, which is installed by default. So if your request is something like
http://127.0.0.1:8080/app/<LOGICAL_NAME_EXTRACTED_BY_VIEW_NAME_TRANSLATOR_GOES_HERE>.html
Have you seen <LOGICAL_NAME_EXTRACTED_BY_VIEW_NAME_TRANSLATOR> ??? So if your request is
http://127.0.0.1:8080/app/login.html
The logical name extracted by ViewNameTranslator is login which is supplied To viewResolver and Translated To
/jsp/login.jsp
Nothing else

Resources