傳統 Java Web 開發模式 (JSP + Controller) 已難以應對複雜交互需求。本文展示一個生產級參考項目:**後端 Spring + Spring MVC + MyBatis 提供 REST API,前端 React + Reflux + Webpack 構建 SPA**,雙端通過 JSON 交互,Maven 統一構建,開箱即用。
webbf/
├─ src/main/java/ # 後端源碼
│ ├─ config/ # Spring/MyBatis/MVC 配置類
│ ├─ controller/ # REST Controller
│ ├─ service/ # 業務邏輯
│ ├─ dao/ # MyBatis Mapper
│ └─ entity/ # 實體類
├─ src/main/resources/ # 配置文件
│ ├─ applicationContext.xml
│ ├─ spring-mvc.xml
│ ├─ mybatis-config.xml
│ └─ mapper/*.xml
├─ src/main/webapp/ # 前端源碼 (Webpack 入口)
│ ├─ src/
│ │ ├─ components/ # React 組件
│ │ ├─ actions/ # Reflux Actions
│ │ ├─ stores/ # Reflux Stores
│ │ ├─ utils/ # 工具函數
│ │ └─ app.js # 入口
│ ├─ index.html
│ └─ package.json # 前端依賴
├─ pom.xml # Maven 總配置
└─ target/ # 構建輸出
@Transactional)@RestController 直接返回 JSON,無視圖解析器ForwardServlet 將所有 /** 轉發到 index.html,支援前端路由刷新web.xml 僅配 DispatcherServlet + ForwardServlet + 編碼過濾器,其餘全 JavaConfig| 技術 | 版本 | 角色 | 關鍵配置 |
|---|---|---|---|
| React | 15.x | UI 組件庫,虛擬 DOM、單向數據流 | ES6 Class 組件、PropTypes 類型檢查 |
| Reflux | 0.4.x | Flux 實現,Action → Store → View 單向流 | Store 監聽 Action,組件監聽 Store |
| Webpack | 2.x | 模塊打包器,支援 JSX/ES6/CSS/圖片 | babel-loader、css-loader、dev-server 熱更新 |
| ES6 | - | 模塊化、箭頭函數、解構、Promise | .babelrc: preset-es2015, preset-react |
| jQuery/Bootstrap/ECharts | 最新 | 輔助庫,通過 npm 安裝 import $ from 'jquery' | Webpack ProvidePlugin 自動注入 $ |
# 1. 進入前端目錄
cd src/main/webapp
# 2. 安裝依賴 (讀取 package.json)
npm install
# 3. 開發模式:熱更新 + Source Map
npm run dev # 相當於 webpack-dev-server --inline --hot
# 4. 生產構建:壓縮、Hash、分包
npm run build # 相當於 webpack --display-error-details -p
// actions/UserActions.js
export default Reflux.createActions([
'loadUsers', // 異步加載
'loadUsersCompleted', // 完成
'loadUsersFailed', // 失敗
'addUser' // 新增
]);
// stores/UserStore.js
import UserActions from '../actions/UserActions';
export default Reflux.createStore({
listenables: UserActions,
init() { this.users = []; },
onLoadUsers() {
// 觸發異步請求
fetch('/api/users')
.then(res => res.json())
.then(data => this.users = data)
.then(() => this.trigger(this.users)) // 通知組件更新
.catch(err => UserActions.loadUsersFailed(err));
},
onAddUser(user) {
fetch('/api/users', {method:'POST', body:JSON.stringify(user)})
.then(() => UserActions.loadUsers()); // 重新加載
}
});
// components/UserList.jsx
import UserStore from '../stores/UserStore';
class UserList extends React.Component {
constructor(props) {
super(props);
this.state = { users: [] };
}
componentDidMount() {
this.unsubscribe = UserStore.listen(users => this.setState({ users }));
UserActions.loadUsers();
}
componentWillUnmount() { this.unsubscribe(); }
render() {
return {this.state.users.map(u => {u.name} )}
;
}
}
state 變化觸發 render(),Diff 算法計算最小更新集,批量寫入真實 DOMclass X extends React.Component,Props 輸入、State 內部、生命週期鉤子

GitHub:https://github.com/peterchenhdu/webbf
ForwardServlet 啟動類cd src/main/webapp && npm install && npm run devhttps://localhost:8080/ 即可看到 React SPA| 原技術 | 建議升級 | 理由 |
|---|---|---|
| Spring 4.x XML 配置 | Spring Boot 2.x/3.x + JavaConfig | 自動配置、啟動快、生態豐富 |
| MyBatis XML | MyBatis-Plus / MyBatis Dynamic SQL | 減少樣板代碼、類型安全 |
| React 15 Class 組件 | React 18 + Hooks (Function Component) | 邏輯復用更強、包體積更小 |
| Reflux | Redux Toolkit / Zustand / Jotai | Reflux 停止維護,Redux 生態主流 |
| Webpack 2 | Webpack 5 / Vite | 構建速度快數倍、配置更簡單 |
| Maven 多模組 | Gradle / Maven 保留 | Gradle 腳本更靈活、增量構建快 |
這個項目雖然技術棧略顯「上一代」,但完整展示了 **前後端分離、REST API、單向數據流、模塊化構建** 的工程化落地全過程。克隆下來跑通,再按遷移表逐項升級,是理解現代 Java 全棧演進最低成本的路徑。