- 文件结构
- struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="admin" namespace="/admin" extends="struts-default" >
<action name="Category_*" class="com.bjsxt.bbs2009.action.CategoryAction" method="{1}">
<result name="success">/admin/Category_{1}.jsp</result>
<result name="input">/admin/Category_{1}.jsp</result>
</action>
</package>
<package name="front" namespace="/" extends="struts-default" >
<default-action-ref name="Category_list"></default-action-ref>
<action name="Category_list" class="com.bjsxt.bbs2009.action.CategoryAction" method="list">
<result>/index.jsp</result>
</action>
</package>
</struts> - 前台--只是一个简单的页面跳转和默认action跳转,在<package name="front">中,其他处理没有
- 后台--
页面:
- index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>北京尚学堂BBS2009论坛管理平台</title>
<link rel="stylesheet" type="text/css"
href="ext/resources/css/ext-all.css" />
<!-- GC -->
<!-- LIBS -->
<script type="text/javascript" src="ext/adapter/ext/ext-base.js">
</script>
<!-- ENDLIBS -->
<script type="text/javascript" src="ext/ext-all.js">
</script>
<script type="text/javascript" src="ext/ext-lang-zh_CN.js">
</script>
<style type="text/css">
html,body {
font: normal 12px verdana;
margin: 0;
padding: 0;
border: 0 none;
overflow: hidden;
height: 100%;
}
.empty .x-panel-body {
padding-top: 0;
text-align: center;
font-style: italic;
color: gray;
font-size: 11px;
}
.x-btn button {
font-size: 14px;
}
.x-panel-header {
font-size: 14px;
}
</style>
<script type="text/javascript">
Ext.onReady( function() {
//Ext.Msg.alert('ext','welcome you!');
var addPanel = function(btn, event) {
var n;
n = tabPanel.getComponent(btn.id);
if(n) {
tabPanel.setActiveTab(n);
return;
}
n = tabPanel.add( {
id : btn.id,
title : btn.id,
html : '<iframe width=100% height=100% src=' + btn.id + ' />',
//autoLoad : '',
closable : 'true'
});
tabPanel.setActiveTab(n);
}
var item1 = new Ext.Panel( {
title : 'Category管理',
//html : '<empty panel>',
cls : 'empty',
items : [
new Ext.Button({
id : 'Category_list',
text : 'Category列表',
width : '100%',
listeners : {
click : addPanel
}
}),
new Ext.Button({
id : 'test',
text : 'Test',
width : '100%',
listeners : {
click : addPanel
}
})
]
});
var item2 = new Ext.Panel( {
title : 'Accordion Item 2',
html : '<empty panel>',
cls : 'empty'
});
var item3 = new Ext.Panel( {
title : 'Accordion Item 3',
html : '<empty panel>',
cls : 'empty'
});
var item4 = new Ext.Panel( {
title : 'Accordion Item 4',
html : '<empty panel>',
cls : 'empty'
});
var item5 = new Ext.Panel( {
title : 'Accordion Item 5',
html : '<empty panel>',
cls : 'empty'
});
var accordion = new Ext.Panel( {
region : 'west',
margins : '5 0 5 5',
split : true,
width : 210,
layout : 'accordion',
items : [ item1, item2, item3, item4, item5 ]
});
var tabPanel = new Ext.TabPanel( {
region : 'center',
enableTabScroll : true,
deferredRender : false,
activeTab : 0,
items : [ {
title : 'index',
//html : 'aaaaaa'
autoLoad : 'Category_add.jsp'
} ]
});
var viewport = new Ext.Viewport( {
layout : 'border',
items : [ accordion, tabPanel ]
});
});
</script>
</head>
<body>
<!-- EXAMPLES -->
</body>
</html>
- 点击Category_list 按钮,根据action查struts.xml, 调用CategoryAction.java, CategoryAction.java执行其中的list() 方法,此方法可以查询数据库中所有的数据,打印显示在Category_list.jsp 页面上
- CategoryAction.java
package com.bjsxt.bbs2009.action;
import java.util.List;
import com.bjsxt.bbs2009.model.Category;
import com.bjsxt.bbs2009.service.CategoryService;
import com.opensymphony.xwork2.ActionSupport;
public class CategoryAction extends ActionSupport{
private List<Category> categories;
private CategoryService CategoryService = new CategoryService();
private String id;
private Category category;
public String list(){
categories = CategoryService.list();
return SUCCESS;
}
public String delete(){
CategoryService.deleteById(id);
return SUCCESS;
}
public String modify(){
CategoryService.modify(category);
return SUCCESS;
}
public String add(){
CategoryService.add(category);
return SUCCESS;
}
public String addInput(){
return INPUT;
}
public String modifyInput(){
category = CategoryService.findById(id);
return INPUT;
}
public List<Category> getCategories() {
return categories;
}
public void setCategories(List<Category> categories) {
this.categories = categories;
}
public CategoryService getCategoryService() {
return CategoryService;
}
public void setCategoryService(CategoryService categoryService) {
CategoryService = categoryService;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
- Category_list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Category_input.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
Category_list
<a href="admin/Category_addInput">添加category</a>
<hr/>
<s:iterator value="categories" var="c">
<s:property value="#c.id"/>|
<s:property value="#c.name"/>|
<s:property value="#c.description"/>|
<a href="admin/Category_modifyInput?id=<s:property value='#c.id'/>"">修改category</a>|
<a href="admin/Category_delete?id=<s:property value='#c.id'/>">删除category</a>
<p>
</s:iterator>
<s:debug></s:debug>
</body>
</html>
- 点击添加category链接,根据<a href="admin/Category_addInput">跳 转至Category_add.jsp
- Category_add.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Category_input.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="admin/Category_add" method="post">
id:<input name = "category.id" /><br>
name:<input name="category.name" /><br>
description:<textarea name="category.description"></textarea><br>
<input type="submit" value="add" />
</form>
</body>
</html> - 根据action="admin/Category_add" 执行CategoryAction.java 执行其中的add()方法,执行页面跳转至
- Category_add.jsp页 面中只有add OK!内容。
- 至此,添加成功。
- 其他的删改操作大同小异。分析清楚流程,才是王道!有助于理解代码和调试。
- index.jsp
- 其他代码
- 删除就直接参照的Category_list.jsp 删除链接,跳转后的页面没有代码
- 修改页面Category_modifyInput.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Category_input.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="admin/Category_modify" method="post">
id:<input name = "category.id" readOnly="true" value="<s:property value='category.id'/>"/><br>
name:<input name="category.name" value="<s:property value='category.name'/>"/><br>
description:<textarea name="category.description"><s:property value="category.description"/></textarea><br>
<input type="submit" value="modify" />
<s:debug></s:debug>
</form>
</body>
</html>
- service层、model层和DB数据库链接管理
- CategoryService.java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.*;
import java.util.List;
import com.bjsxt.bbs2009.model.Category;
import com.bjsxt.bbs2009.util.*;
public class CategoryService {
public void add(Category c){
Connection conn = DB.createConn();
String sql = "insert into t_category values(?,?,?)";
PreparedStatement pstmt = DB.prepare(conn, sql);
try {
pstmt.setString(1, c.getId());
pstmt.setString(2, c.getName());
pstmt.setString(3, c.getDescription());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
DB.close(pstmt);
DB.close(conn);
}
public List<Category> list(){
Connection conn = DB.createConn();
String sql = "select * from t_category";
PreparedStatement pstmt = DB.prepare(conn, sql);
List<Category> categories = new ArrayList<Category>();
ResultSet rs = null;
try {
rs = pstmt.executeQuery();
Category c = null;
while(rs.next()){
c = new Category();
c.setId(rs.getString("id"));
c.setName(rs.getString("name"));
c.setDescription(rs.getString("description"));
categories.add(c);
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
DB.close(pstmt);
DB.close(conn);
return categories;
}
public void delete(Category c){
deleteById(c.getId());
}
public void deleteById(String id){
Connection conn = DB.createConn();
String sql = "delete from t_category where id = ?";
PreparedStatement pstmt = DB.prepare(conn, sql);
try {
pstmt.setString(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
DB.close(pstmt);
DB.close(conn);
}
public void modify(Category c){
Connection conn = DB.createConn();
String sql = "update t_category set name = ?,description = ? where id = ?";
PreparedStatement pstmt = DB.prepare(conn, sql);
try {
pstmt.setString(1, c.getName());
pstmt.setString(2, c.getDescription());
pstmt.setString(3, c.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
DB.close(pstmt);
DB.close(conn);
}
public Category findById(String id){
Connection conn = DB.createConn();
String sql = "select * from t_category where id = ?";
PreparedStatement pstmt = DB.prepare(conn, sql);
Category c = null;
ResultSet rs = null;
try {
pstmt.setString(1,id);
rs = pstmt.executeQuery();
c = new Category();
if(rs.next()){
c = new Category();
c.setId(rs.getString("id"));
c.setName(rs.getString("name"));
c.setDescription(rs.getString("description"));
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
DB.close(pstmt);
DB.close(conn);
return c;
}
public static void main(String[] args){
Category c = new Category();
c.setId("cccc");
c.setName("aaaaaaaaaa");
c.setDescription("description");
// List list = new CategoryService().list();
// for(Iterator it = list.iterator();it.hasNext();){
// Category c = (Category) it.next();
// System.out.println(c.getId()+" "+c.getName()+" "+c.getDescription());
// }
// Category c = new CategoryService().findById("aaaa");
// System.out.println(c.getId());
// System.out.println(c.getName());
// System.out.println(c.getDescription());
// System.out.println("OK!");
}
}
- Category.java
package com.bjsxt.bbs2009.model;
public class Category {
private String id = null;
private String name = null;
private String description = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
- DB.java
package com.bjsxt.bbs2009.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.resource.cci.ResultSet;
public class DB {
public static Connection createConn(){
Connection conn = null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=webjsly";
String username = "sa";
String password = "jsly";
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static PreparedStatement prepare(Connection conn,String sql){
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static void close(Connection conn){
try {
conn.close();
conn = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(PreparedStatement pstmt){
try {
pstmt.close();
pstmt = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs){
try {
rs.clone();
rs = null;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
struts2 初学(三)BBS增删改查
最新推荐文章于 2010-11-25 19:20:00 发布