Comprehensive Guide to Developing a Traditional Java Web Project
This article outlines the essential steps for creating a traditional Java web application, covering environment preparation, Maven skeleton creation, required dependencies for logging, databases, persistence, Spring integration, DAO and service layers, unit testing, and RESTful controller configuration.
This article provides a detailed checklist for developing a traditional Java Web project, from environment preparation to three‑tier architecture implementation.
Part 1: Environment Preparation
1. Create a web skeleton using Maven: mvn archetype:create -DgroupId=net.rocketa -DartifactId=mywebapp -DarchetypeArtifactId=maven-archetype-webapp
2. In IntelliJ IDEA, set Maven project properties for Sources , Tests , and Resources/Test Resources .
3. Add logging dependencies: use the slf4j API together with an implementation such as log4j , logback or commons‑logging .
4. Include database dependencies: a JDBC driver JAR (runtime only) and a connection‑pool library (e.g., C3P0, DBCP).
5. Add a persistence framework dependency (MyBatis, Hibernate, etc.) and, if needed, the Spring‑MyBatis integration libraries.
6. Add Web‑layer dependencies such as JSTL, tag libraries, Ajax JSON support, and the servlet-api for compilation.
7. Include core Spring modules: spring-core , spring-beans , spring-context .
8. Add Spring‑JDBC and Spring‑Transaction for database interaction and declarative transaction management.
9. Add Spring‑Web and Spring‑WebMVC for web integration.
10. Add spring-test to support JUnit testing with Spring.
Part 2: DAO Layer
• Create SQL files for table definitions, ensuring InnoDB storage engine, auto‑increment, proper charset, primary keys, indexes, and a created_at timestamp column. • Document table and column comments for team readability. • Use INSERT IGNORE to avoid errors on unique‑key conflicts.
• Define entity classes with getters, setters, and toString , handling one‑to‑one, one‑to‑many, and many‑to‑many relationships.
• Design DAO interfaces and MyBatis mapper XML files. Example MyBatis features: • SQL in XML (preferred) or annotations. • Global MyBatis config: camel‑case mapping, generated keys, alias packages. • Use #{} placeholders and CDATA sections where needed. • Specify parameterType and resultType correctly; use @Param for multiple primitive parameters. • OGNL expressions can set nested object properties. • Table aliases are ignored by MyBatis, so table.column as alias works transparently. Summary: DAO work focuses on interface design and SQL writing, separating code from queries for easier review.
Part 3: Spring JUnit Testing
Use Spring’s test runner and context configuration: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"classpath:spring-config.xml"}) After the Spring container loads, beans can be injected and tested directly.
Part 4: Service Layer
Key concerns include DTO design, business‑exception handling (extend RuntimeException because Spring rolls back on unchecked exceptions), and transaction management. Typical try‑catch pattern: <code>try { // business logic } catch (BusinessException1 e1) { // handle specific business error } catch (BusinessException2 e2) { // handle another business error } catch (Exception e) { throw new BusinessException(...); }</code>
Part 5: Controller Layer
RESTful URI design examples: GET /product/list – query list GET /product/{productId}/detail – get details POST /product/{productId}/execution – create or update (non‑idempotent) DELETE /product/{productId}/delete – delete PUT /product/{productId} – update (idempotent) Typical Spring MVC method signature: <code>@RequestMapping(value="/{productId:\\d+}/detail", method=RequestMethod.GET, produces={"application/json;charset=UTF-8"}) @ResponseBody public String detail(@PathVariable("productId") Long productId, @CookieValue(value="userId", required=false) Long userId, Model model) { // return "redirect:/xxx/yyy" or "forward:/xxx/yyy" or view name } </code> Configure Spring MVC in web.xml with DispatcherServlet , enable annotation‑driven MVC ( <mvc:annotation-driven/> ), default servlet handling ( <mvc:default-servlet-handler/> ), and set up a ViewResolver for JSPs.
Overall, the article consolidates the practical steps and best practices needed to set up, develop, test, and deploy a conventional Java web application.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
