Web 开发项目的6个最佳Java框架(web开发框架)
ccwgpt 2024-11-03 12:31 55 浏览 0 评论
Spring 也是开源轻量级的IOC和AOP的容器框架
1.IOC(Inverse of Control)控制反转是Spring的核心模块。(面向接口的编程)
2.AOP(Aspect-Oriented Programming)面向切面的编程,除了IOC容器,Spring的另一个核心模块就是我们AOP框架了。(当然我们的Spring的IOC模块并不依赖于AOP)
接下来就让Spring结合之前的Struts2来创建一个小项目吧
a.在我们的lib目录下导入Spring的依赖包(Spring在与Struts2结合使用时需要导入两个包commons-logging-1.1.3.jar和struts2-spring-plugin-2.3.30)
这样我们的包就已经导好了。
b.将Spring配置文件加载到Web进行解析就需要在Web.xml的配置文件里边写解析的代码:
(我们老师说:“Spring 就像一只鹰,在天空中俯视着,管理着我们项目的每一个实例”)
c.Web.xml的配置文件已经写好了,接下来就需要在src目录下创建util包,entity包,dao包,service包,action包。
1.在java_util包里边创建我们的JavaConnection的类,在这边我就直接写JavaConnection的实现类(JavaConnectionImpl):
package java_util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JavaConnectionImpl implements JavaConnection {
//获取数据库的连接(加载驱动,URL地址,访问数据库的名字,访问数据库的密码,connection连接变量)
private String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String url="jdbc:sqlserver://localhost:1433;DatabaseName=SSH";
private String name="sa";
private String pwd="123";
private Connection conn=null;
@Override
public Connection getAllConnection() {
try {
//加载驱动
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
//获取数据库连接
conn=DriverManager.getConnection(url,name,pwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}
2.在entity包创建我们的实体类:
package java_entity;
import java.math.BigDecimal;
import java.sql.Date;
public class BookJava {
private int cid; //编号
private String name; // 书本名称
private Date BookDate; //出版日期
private BigDecimal deposit; //价格
public BookJava() {
super();
}
public BookJava(int cid, String name, Date bookDate, BigDecimal deposit) {
super();
this.cid = cid;
this.name = name;
BookDate = bookDate;
this.deposit = deposit;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBookDate() {
return BookDate;
}
public void setBookDate(Date bookDate) {
BookDate = bookDate;
}
public BigDecimal getDeposit() {
return deposit;
}
public void setDeposit(BigDecimal deposit) {
this.deposit = deposit;
}
}
3.在Java_dao包创建访问数据库的类:
package ssh.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java_entity.BookJava;
import java_util.MyConnection;
import java_util.MyConnectionImpl;
public class JavaBookDaoImpl implements JavaBookDao {
//给dao注入JavaConnection的类(往后学习了hibernate将会自动完成JDBC的写法)
private JavaConnection c = null;
public void setC(JavaConnection c) {
this.c = c;
}
@Override
public List<BookJava> getAllBookJava() {
//1.dao这一层只做访问数据库
//2.任何与数据库无关的代码,统统不参与
MyConnection c = new MyConnectionImpl();
Connection conn = c.getConnection();
// 第二步:查询数据库
// (操作JDBC,需要sql语句,需要执行sql语句对象,需要执行sql语句后结果集合)
String sql = "select * from BookJava";// 需要执行的sql语句
PreparedStatement stmt = null;// 执行sql语句对象
ResultSet rs = null;// 执行sql语句后的结果集
try {
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
} catch (Exception e) {
System.out.println(e.getMessage());;
}
// 2.拿到rs后,执行一个遍历以及封装
List myBookJavaList = new ArrayList<BookJava>();
try {
while (rs.next()) {
// 风格:实体类是一个pojo对象,一般直接new就行
BookJava bc = new BookJava();
// 进行数据库封装
bc.setCid(rs.getInt("cid"));
bc.setName(rs.getString("name"));
bc.setBookDate(rs.getDate("BookDate"));
bc.setDeposit(rs.getBigDecimal("deposit"));
myBookCardList.add(bc);// 添加到list集合
}
} catch (Exception e1) {
e1.printStackTrace();
}
// 第四步(关闭外部资源【数据库、文件、网络资源】)
try {
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return myBookJavaList;
}
}
4.在java_service包写业务逻辑类:
package java_service;
import java.util.List;
import java_dao.JavaBookDao;
import java_entity.BookJava;
public class JavaServiceImpl implements JavaService {
//(spring)注入dao包里实现类的实例
private JavaBookDao jd;
public void setJd(JavaBookDao jd) {
//在控制台输出注入的实例
System.out.println("注入dao的实例"+jd);
this.jd = jd;
}
public List<BookJava> getAllJavaBook() {
// TODO Auto-generated method stub
List<BookJava> JavaBook=jd.getAllJavaBookDao();
return JavaBook;
}
}
5.在java_Action包写action类:
package java_action;
import java.text.DecimalFormat;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java_entity.BookJava;
import java_service.JavaService;
public class JavaBookAction extends ActionSupport {
//注入service实例
private JavaService jsi;
public void setJsi(JavaService jsi) {
this.jsi = jsi;
}
public String execute(){
List<BookJava> javaList=jsi.getAllJavaBook();
System.out.println("结果集:"+javaList.size());
ActionContext ac=ActionContext.getContext();
ac.put("javaList",javaList);
return SUCCESS;
}
//输出人民币符号
public String formatDouble(double s){
DecimalFormat df=new DecimalFormat("u00A4##.0");
return df.format(s);
}
}
6.将所有的类都注入实例后,需要写Spring的配置文件(Spring将会对所有的实例进行管理)在src的目录下创建applicationContext.xml配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<bean id="javaBookAction" class="java_action.JavaBookAction" scope="prototype">
<property name="jsi" ref="javaService"></property>
</bean>
<!--javaService = new java_service.JavaServiceImpl() -->
<!-- javaService 的class相当于是new出来的java_service.JavaServiceImpl()-->
<bean id="javaService" class="java_service.JavaServiceImpl" scope="prototype">
<property name="jd" ref="javaBookdaoImpl"></property>
</bean>
<bean id="javaBookdaoImpl" class="java_dao.JavaBookDaoImpl" scope="prototype">
<property name="c" ref="JavaConnectionImpl"></property>
</bean>
<bean id="JavaConnectionImpl" class="java_unit.JavaConnectionImpl" scope="prototype"></bean>
</beans>
7.附上Struts.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 告知Struts2运行时使用Spring来创建对象 -->
<constant name="struts.objectFactory" value="spring"></constant>
<package name="javaPackage" extends="struts-default" namespace="/">
<action name="index" class="javaBookAction" method="execute">
<result name="success">/WEB-INF/jsp/javaIndex.jsp</result>
</action>
</package>
</struts>
8.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1px">
<tr>
<th>编号</th>
<th>书名</th>
<th>出版日期</th>
<th>课本价格</th>
</tr>
<s:iterator value="javaList">
<tr>
<td><s:property value="cid"/></td>
<td><s:property value="name"/></td>
<td><s:date name="BookDate" format="yyyy-MM-dd"></s:date></td>
<td><s:property value="%{formatDouble(deposit)}" /></td>
</tr>
</s:iterator>
<s:if test="javaList.size()==0">
<tr>
<td colspan="7">没有查找到数据</td>
</tr>
</s:if>
</table>
</body>
</html>
9.最后输出页面
基本的Spring框架就已经搭建好了,其实注入也是IOC的另一个说法。Spring的注入降低了类与类之间的耦合度,提高了类内部的内聚度,使我们的软件具有更容易扩展和维护。
SpringAOP将会在后面SSH框架搭完后再进行详细的介绍。
相关推荐
- 一个基于.Net Core遵循Clean Architecture原则开源架构
-
今天给大家推荐一个遵循CleanArchitecture原则开源架构。项目简介这是基于Asp.netCore6开发的,遵循CleanArchitecture原则,可以高效、快速地构建基于Ra...
- AI写代码翻车无数次,我发现只要提前做好这3步,bug立减80%
-
写十万行全是bug之后终于找到方法了开发"提示词管理助手"新版本那会儿,我差点被bug整崩溃。刚开始两周,全靠AI改代码架构,结果十万行程序漏洞百出。本来以为AI说没问题就稳了,结果...
- OneCode低代码平台的事件驱动设计:架构解析与实践
-
引言:低代码平台的事件驱动范式在现代软件开发中,事件驱动架构(EDA)已成为构建灵活、松耦合系统的核心范式。OneCode低代码平台通过创新性的注解驱动设计,将事件驱动理念深度融入平台架构,实现了业务...
- 国内大厂AI插件评测:根据UI图生成Vue前端代码
-
在IDEA中安装大厂的AI插件,打开ruoyi增强项目:yudao-ui-admin-vue31.CodeBuddy插件登录腾讯的CodeBuddy后,大模型选择deepseek-v3,输入提示语:...
- AI+低代码技术揭秘(二):核心架构
-
本文档介绍了为VTJ低代码平台提供支持的基本架构组件,包括Engine编排层、Provider服务系统、数据模型和代码生成管道。有关UI组件库和widget系统的信息,请参阅UI...
- GitDiagram用AI把代码库变成可视化架构图
-
这是一个名为gitdiagram的开源工具,可将GitHub仓库实时转换为交互式架构图,帮助开发者快速理解代码结构。核心功能一键可视化:替换GitHubURL中的"hub...
- 30天自制操作系统:第六天:代码架构整理与中断处理
-
1.拆开bootpack.c文件。根据设计模式将对应的功能封装成独立的文件。2.初始化pic:pic(可编程中断控制器):在设计上,cpu单独只能处理一个中断。而pic是将8个中断信号集合成一个中断...
- AI写代码越帮越忙?2025年研究揭露惊人真相
-
近年来,AI工具如雨后春笋般涌现,许多人开始幻想程序员的未来就是“对着AI说几句话”,就能轻松写出完美的代码。然而,2025年的一项最新研究却颠覆了这一期待,揭示了一个令人意外的结果。研究邀请了16位...
- 一键理解开源项目:两个自动生成GitHub代码架构图与说明书工具
-
一、GitDiagram可以一键生成github代码仓库的架构图如果想要可视化github开源项目:https://github.com/luler/reflex_ai_fast,也可以直接把域名替换...
- 5分钟掌握 c# 网络通讯架构及代码示例
-
以下是C#网络通讯架构的核心要点及代码示例,按协议类型分类整理:一、TCP协议(可靠连接)1.同步通信//服务器端usingSystem.Net.Sockets;usingTcpListene...
- 从复杂到优雅:用建造者和责任链重塑代码架构
-
引用设计模式是软件开发中的重要工具,它为解决常见问题提供了标准化的解决方案,提高了代码的可维护性和可扩展性,提升了开发效率,促进了团队协作,提高了软件质量,并帮助开发者更好地适应需求变化。通过学习和应...
- 低代码开发当道,我还需要学习LangChain这些框架吗?| IT杂谈
-
专注LLM深度应用,关注我不迷路前两天有位兄弟问了个问题:当然我很能理解这位朋友的担忧:期望效率最大化,时间用在刀刃上,“不要重新发明轮子”嘛。铺天盖地的AI信息轰炸与概念炒作,很容易让人浮躁与迷茫。...
- 框架设计并不是简单粗暴地写代码,而是要先弄清逻辑
-
3.框架设计3.框架设计本节我们要开发一个UI框架,底层以白鹭引擎为例。框架设计的第一步并不是直接撸代码,而是先想清楚设计思想,抽象。一个一个的UI窗口是独立的吗?不是的,...
- 大佬用 Avalonia 框架开发的 C# 代码 IDE
-
AvalonStudioAvalonStudio是一个开源的跨平台的开发编辑器(IDE),AvalonStudio的目标是成为一个功能齐全,并且可以让开发者快速使用的IDE,提高开发的生产力。A...
- 轻量级框架Lagent 仅需20行代码即可构建自己的智能代理
-
站长之家(ChinaZ.com)8月30日消息:Lagent是一个专注于基于LLM模型的代理开发的轻量级框架。它的设计旨在简化和提高这种模型下代理的开发效率。LLM模型是一种强大的工具,可以...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- 框架图 (58)
- flask框架 (53)
- quartz框架 (51)
- abp框架 (47)
- springmvc框架 (49)
- 分布式事务框架 (65)
- scrapy框架 (56)
- shiro框架 (61)
- 定时任务框架 (56)
- java日志框架 (61)
- mfc框架 (52)
- abb框架断路器 (48)
- beego框架 (52)
- java框架spring (58)
- grpc框架 (65)
- tornado框架 (48)
- 前端框架bootstrap (54)
- orm框架有哪些 (51)
- 知识框架图 (52)
- ppt框架 (55)
- 框架图模板 (59)
- 内联框架 (52)
- cad怎么画框架 (58)
- ssm框架实现登录注册 (49)
- oracle字符串长度 (48)