Web Development
Articles, notes, and tutorials filed under Web Development.
Latest articles
Showing 17-22 of 22Unit Testing for Spring MVC Controllers
Once the services, DAO, and supporting classes are tested, it’s time for the controller. Controllers are often the hardest layer to test, so many developers (based on observation) reach for Selenium or, worse, test by hand. That works, but it makes exercising individual logic branches awkward and slow, and no one wants to wait for a browser to spin up before checking in code. The Spring MVC Test framework solves this by driving full controller logic through fast unit tests, and it has lived in Spring’s core ever since Spring 4.
Spring 4 MVC Hello World Tutorial Using Maven
In this tutorial we build the smallest useful Spring MVC application: a controller that reads a request parameter and renders it in a JSP view. It is the classic “Hello World”, but the wiring it shows (a DispatcherServlet, a bean configuration, and a view resolver) is the same wiring every larger Spring MVC application is built on. Note: This tutorial has been updated for Spring Framework 7. It was originally written in 2014 for Spring 4 and the javax.* servlet API; the configuration below now uses the jakarta.* namespace, drops the Eclipse-specific setup for a plain Maven command, and runs on Jakarta EE 11 (Tomcat 11+). We keep XML configuration and web.xml so every piece of the wiring stays visible. The same beans map one-to-one onto a @Configuration class if you prefer Java config.
Zend Framework 2: Get Parameters
The easiest way to read request parameters in a Zend Framework 2 controller is the Params plugin, introduced in beta5. It has utility methods to make it easy to access different types of parameters. As always, reading the tests can prove valuable to understand how something is supposed to be used. Note: Zend Framework 2 has been deprecated. The project moved to the Linux Foundation in 2019 and lives on as Laminas; the Zend\ namespace became Laminas\, so the plugin shown here is now Laminas\Mvc\Controller\Plugin\Params (in the laminas/laminas-mvc package). The API is unchanged, so the examples below still apply after that rename.
Zend Framework 2: Redirect to 404 page in Controller
I’ve been getting into trouble for several hours with redirecting to the 404 page in Zend Framework 2. Note: Zend Framework 2 has been deprecated. Zend Framework was migrated to the Laminas Project in 2020, where development continues today. The notFoundAction() technique below still applies in laminas-mvc. I asked about this on Stack Overflow. What Didn’t Work Before that, I was using the following code: $this->getResponse()->setStatusCode(404); return; This sets the HTTP status code to 404, which looks fine at first. But it only changes the response header; it doesn’t dispatch the error controller, so once I built a custom 404 page I found that it never rendered.
Use CodeIgniter Resources within Library
To access CodeIgniter’s native resources from inside your own library, use the get_instance() function. It returns the CodeIgniter super object, the same one you reach through $this inside a controller, so your library can load helpers, read config, or call any other class the framework has instantiated. Note: get_instance() is a CodeIgniter 3.x (and earlier) technique. CodeIgniter 4 dropped the global super object. There you reach framework features through Services, such as service('session') or \Config\Services::session(), or by injecting the dependencies your class needs through its constructor. Everything below applies to CodeIgniter 3.x.
Generate a Random File Name when Uploading Files in WordPress
The passage is telling you how to rename the file name automatically by WordPress (without using any plugins). A Word of Warning Warning: To apply this hack, you’ll have to edit one of WordPress’s core files. Keep in mind that it is never recommended. This hack should be redone if you upgrade WordPress. Editing the Upload Handler Open the wp-admin/includes/files.php file, and go to line 324 (approximately here). You’ll see the following: