Can <security-constraint> tag in web.xml be dynamically generated or written outside web.xml? - web.xml

I met a problem, I want to set the tag of security-constraint according to my configuration file dynamically, but I can't do it. So I hope tag in web.xml can be dynamically generated or written outside web.xml. Thanks a lot for your help!

I think your question could be related to this one. However, if you were working with Servlet 3.0 spec, you could try the approach of programmatically adding and configuring security for the servlet, as shown here.

Related

What is the best way to read application-context.xml in Spring MVC application, Is it by adding in init-param tag or by adding in context-param tag?

In few applications I see application-context file being read via init-param and in few via context-param. My intention is to know which one to use when.
after all it dependence on you project requirement
1) if you want to set the access scope of data then use the .if you declare anything inside is only accessible only for that particular servlet. The init-param is declared inside the tag.
2) if you want to access the data whole application or anywhere in the application then you must go with
if you agree with this hit the up-arrow button

How to make an AJAX call in Magnolia Blossom?

I am new to Magnolia Blossom.
I have to perform an AJAX call in my application in Blossom.
We have a Controller per component. So I am unable to make an AJAX request.
Can anyone suggest how can I achieve this?
you may define a different Servlet (and web application context), you can define a servlet that handles all the requests that start for /rest/* and then below that on your web.xml you can define the blossom servlet.
All the rest is configuration, try to see how to create a webapp with 2 different contexts.
The controllers you're using for rendering content are not accessible to requests coming in to the servlet container. Without content they're pretty useless and won't generate meaningful output. You need a separate DispatcherServlet handling these AJAX requests.
There's two ways to achieve this. You can either add a new DispatcherServlet to web.xml or you can add a servlet to your module that is installed when your module is installed.
The latter is the better choice because you won't need to have two separate ApplicationContexts. The one created in your module on startup will be the parent of both DispatcherServlets so both can access beans within it. You also won't need to update web.xml which makes module install easier and upgrades of Magnolia easier.
In your module descriptor add this snippet:
<servlets>
<servlet>
<name>dispatcher</name>
<class>org.springframework.web.servlet.DispatcherServlet</class>
<mappings>
<mapping>/ajax/*</mapping>
</mappings>
<params>
<param>
<name>contextConfigLocation</name>
<value>classpath:/ajax-servlet.xml</value>
</param>
</params>
</servlet>
</servlets>
This, and other topics, is described in the Magnolia wiki https://wiki.magnolia-cms.com/display/WIKI/Adding+a+DispatcherServlet+to+a+Blossom+module
Depending on what you want to get out, you can also use REST module of Magnolia. To e.g. read a title of website you can just call http://localhost:8080/magnoliaAuthor/.rest/properties/v1/website/demo-project/siteTitle more details at documentation
And you can use REST module to also very easily add your own endpoints by just annotating source code.
HTH,
Jan

Can I move entire web.xml to spring code base configuration? (WebApplicationInitializer)

My application will use mainly use code based configuration. From web.xml to Springs WebApplicationInitializer class I already moved: servlet, filters and mapping. However in web.xml have much more elements (such as error-page or welcome page: https://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/web_xml.html). Which of those elements I can move to code and what are their equivalents?
Generally, yes, you should be able to get Java Config equivalents of all elements that you can set in the web.xml. In terms of converting individual elements, if you can give specifics, then we could find the JavaConfig equivalents.
Note that it's not always a 1-to-1 mapping. For example, with Spring Security, you have to define the "delegatingFilterProxy" filter in web.xml but the JavaConfig equivalent does this behind the scenes. Using Spring Security annotations triggers that behavior.
I recommend two things:
Read through the Spring JavaConfig guide if you haven't done so
If you're stuck on a particular element, search for " JavaConfig". Usually, you can find some useful information quickly that way.
For the welcome page follow these steps:
Under your application root, create a html file called index.html
In that declare a head section with the content: <meta http-equiv="Refresh" content="0; URL=anonymous/homepage.htm"/>
Now when you access your application using http://myapplication.com you will be automatically redirected to http://myapplication.com/anonymous/homepage.htm. This acts as your index/welcome page
For the error page follow these steps:
In the controller:
try
{
}
catch (Exception ex)
{
return new ModelAndView("error");
}
In views.properties (or equivalent) define a error page like:
error.(class)=org.springframework.web.servlet.view.JstlView
error.url=/WEB-INF/jsp/errorpage.jsp

Get config setting in custom taglib using Spring "context-property-placeholder"

I'm creating a custom taglib, and would like to use some config options that are loaded via the underlying Spring framework using:
<context:property-placeholder location="classpath:config.properties" />
How would I get access to these variables in my taglib?
Thanks,
James.
The JSP taglibs have nothing in common with the Spring context's lifecycle, they're managed by the servlet container. This can complicate things a bit, for example: inject-dependency-into-a-taglib-class, how-to-write-tag-in-my-spring-project.
Since you're only mentioning the need for contents of the properties file, you could use plain old java.util.ResourceBundle (or, if you need more flexibility, Apache Commons' org.apache.commons.configuration.PropertiesConfiguration).
(One could also argue that requiring access to configuration in your tags indicates a design problem...)

ZK - inject Spring beans directly into ZK beans

Is it possible to inject Spring beans directly into ZK backing beans?
In tutorials such as this I've found only an example, where application context was extracted manually from web application complex, which very unelegant and unflexible.
In fact, the VariableResolver is working, it is however hard to find, how to use it properly.
First, I had to include header in .zul file:
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
Then, use ${controllerBeanName} in apply attribute, f.e.
<window id="win" title="Typy mieszkaƄ" width="750px" border="normal"
apply="${appartmentTypeController}">
In my case, appartmentTypeController is bean extending GeneralForwardComposer, defined in spring context.
did you try the Variable-Resolver ?? i found an article about it
http://books.zkoss.org/wiki/Small_Talks/2010/December/Integrate_ZK_Spreadsheet2.0.0_with_Spring
May ZK-DL http://zk.datalite.cz/zk-dl library help you? It takes it's own approach to Spring integration, not the original ZK way.

Resources