- 浏览: 715067 次
- 性别:
- 来自: 上海
最新评论
-
TheUniqueGirl:
Tomcat系统架构与设计模式:http://www.doci ...
Tomcat -
aykjy:
...
UML常见工具之NetBeans(downmoon) -
不老肖邦:
谢谢提供的简单示例
JAVA toString()
<%@ 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-update" method="post">
<input type="hidden" name="category.id" value="<s:property value="category.id"/>" />
name:<input name="category.name" value="<s:property value="category.name"/>" />
description:<textarea name="category.description"><s:property value="category.description"/></textarea>
<input type="submit" value="update" />
</form>
</body>
</html>
1. 建立界面原型
2. 建立Struts.xml
a) 确定namespace
b) 确定package
c) 确定Action的名称,空的方法
d) 确定Result
e) 将界面原型页面进行修改,匹配现有设置
f) 测试
g) 做好规划!!!!!
3. 建立数据库(或者实体类)
4. 建立Model层
5. 建立Service层(后面讲了Hibernate后再完善)
a) 此时可以使用JUnit进行单元测试了
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>
<constant name="struts.devMode" value="true"></constant>
<package name="bbs2009_default" extends="struts-default">
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
</package>
<package name="admin" namespace="/admin" extends="bbs2009_default" >
<action name="index">
<result>/admin/index.html</result>
</action>
<action name="*-*" class="com.bjsxt.bbs2009.action.{1}Action" method="{2}">
<result>/admin/{1}-{2}.jsp</result>
<result name="input">/admin/{1}-{2}.jsp</result>
<!-- <exception-mapping result="error" exception="java.sql.SQLException" />
<result name="error">/error.jsp</result> -->
</action>
<!--
<action name="category" class="com.bjsxt.bbs2009.action.CategoryAction">
<result>/admin/category_list.jsp</result>
<result name="add_input">/admin/category_add_input.jsp</result>
<result name="update_input">/admin/category_update_input.jsp</result>
</action>
-->
</package>
<package name="front" namespace="/" extends="struts-default" >
<default-action-ref name="Category_list" />
<action name="Category_list" class="com.bjsxt.bbs2009.action.CategoryAction" method="list">
<result>/index.jsp</result>
</action>
</package>
</struts>
DB.java
package com.bjsxt.bbs2009.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DB {
public static Connection createConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/bbs2009", "root", "bjsxt");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static PreparedStatement prepare(Connection conn, String sql) {
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return ps;
}
public static void close(Connection conn) {
try {
conn.close();
conn = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement stmt) {
try {
stmt.close();
stmt = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs) {
try {
rs.close();
rs = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Catagary.java(model)
package com.bjsxt.bbs2009.model;
public class Category {
private int id;
private String name;
private String description;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
}
CatagaryService.java
package com.bjsxt.bbs2009.service;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.bjsxt.bbs2009.model.Category;
import com.bjsxt.bbs2009.util.DB;
public class CategoryService {
public void add(Category c) {
Connection conn = DB.createConn();
String sql = "insert into _category values (null, ?, ?)";
PreparedStatement ps = DB.prepare(conn, sql);
try {
ps.setString(1, c.getName());
ps.setString(2, c.getDescription());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
DB.close(ps);
DB.close(conn);
}
public List<Category> list() throws SQLException {
Connection conn = DB.createConn();
String sql = "select * from _category_";
PreparedStatement ps = DB.prepare(conn, sql);
List<Category> categories = new ArrayList<Category>();
try {
ResultSet rs = ps.executeQuery();
Category c = null;
while(rs.next()) {
c = new Category();
c.setId(rs.getInt("id"));
c.setName(rs.getString("name"));
c.setDescription(rs.getString("description"));
categories.add(c);
}
} catch (SQLException e) {
e.printStackTrace();
throw(e);
}
DB.close(ps);
DB.close(conn);
return categories;
}
public void delete(Category c) {
deleteById(c.getId());
}
public void deleteById(int id) {
Connection conn = DB.createConn();
String sql = "delete from _category where id = ?";
PreparedStatement ps = DB.prepare(conn, sql);
try {
ps.setInt(1, id);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
DB.close(ps);
DB.close(conn);
}
public void update(Category c) {
Connection conn = DB.createConn();
String sql = "update _category set name = ?, description = ? where id = ?";
PreparedStatement ps = DB.prepare(conn, sql);
try {
ps.setString(1, c.getName());
ps.setString(2, c.getDescription());
ps.setInt(3, c.getId());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
DB.close(ps);
DB.close(conn);
}
public Category loadById(int id) {
Connection conn = DB.createConn();
String sql = "select * from _category where id = ?";
PreparedStatement ps = DB.prepare(conn, sql);
Category c = null;
try {
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if(rs.next()) {
c = new Category();
c.setId(rs.getInt("id"));
c.setName(rs.getString("name"));
c.setDescription(rs.getString("description"));
}
} catch (SQLException e) {
e.printStackTrace();
}
DB.close(ps);
DB.close(conn);
return c;
}
}
CatagaryAction.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 Category category;
private int id;
public String list() throws Exception {
categories = categoryService.list();
return SUCCESS;
}
public String add() {
categoryService.add(category);
return SUCCESS;
}
public String update() {
categoryService.update(category);
return SUCCESS;
}
public String delete() {
categoryService.deleteById(id);
return SUCCESS;
}
public String addInput() {
return INPUT;
}
public String updateInput() {
this.category = this.categoryService.loadById(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) {
this.categoryService = categoryService;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public int getId() {
return id;
}
public void setId(int 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>
<a href="admin/Category-updateInput">更新Category</a>
<hr/>
<s:iterator value="categories" var="c">
<s:property value="#c.name"/> |
<s:property value="#c.description"/> |
<a href="admin/Category-delete?id=<s:property value="#c.id"/>">删除Category</a> |
<a href="admin/Category-updateInput?id=<s:property value="#c.id"/>">更新Category</a>
<br/>
</s:iterator>
<s:debug></s:debug>
</body>
</html>
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>
add ok!
</body>
</html>
Category-addInput.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">
name:<input name="category.name" />
description:<textarea name="category.description"></textarea>
<input type="submit" value="add" />
</form>
</body>
</html>
Category-delete.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>
Category_delete
</body>
</html>
Category-updateInput.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-update" method="post">
<input type="hidden" name="category.id" value="<s:property value="category.id"/>" />
name:<input name="category.name" value="<s:property value="category.name"/>" />
description:<textarea name="category.description"><s:property value="category.description"/></textarea>
<input type="submit" value="update" />
</form>
</body>
</html>
Category-update.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>
update ok!
</body>
</html>
mysql.sql
create database bbs2009;
use bbs2009;
create table _category(id int primary key auto_increment, name varchar(50), descrtiption varchar(200));
- struts2_BBS2009ExceptionHandling.rar (4.7 MB)
- 下载次数: 0
发表评论
-
Myeclipse的tyry catch 怎么让它自动生成
2011-03-21 19:58 1641方法1:选择代码段,右键单击,选择Surround Wit ... -
深入Struts2
2011-02-24 21:24 8101.深入Struts2的配置文件 本部分主要介绍struts ... -
Struts教程
2011-02-24 22:11 995概述 本文主要讲解什么是Struts Frame ... -
Action接收参数的3种方式(属性,域模型,模型驱动,struts2.1.8)
2011-02-24 22:32 1671常用第一种跟第二种 1.Action属性传参数: ... -
struts2采用域模型方式接收参数
2011-02-24 22:38 1013struts.xml: <struts&g ... -
Hibernate学习笔记(一)--用MyEclipse 6.5+MySQL 5.0的环境跑起来
2011-02-25 14:10 1268准备:建表 用MySQL在名为STMS数据库 ... -
在MyEclipse下如何添加hibernate支持(2)
2011-02-25 14:26 1167在上一篇文章里我们已经成功的为项目添加了hib ... -
在MyEclipse下如何添加hibernate支持(1)
2011-02-25 14:26 1667在网络资源中有很多S2HS整合的文档和教程,但是真正适 ... -
Struts2+Spring+Hibernate搭建全解!
2011-02-26 20:46 766Struts2+Spring+Hibernate是J2 ... -
struts2拦截器详解实例
2011-03-21 16:11 1073struts.xml <?xml version=&qu ... -
Struts2拦截器配置
2011-03-21 11:25 1398拦截器(interceptor)是Struts2最强大的特性之 ... -
Struts2使用之拦截器实例
2011-03-21 11:16 948实例一:防止表单重复提交 防止重复提交:有两种方法:一是使用 ... -
Struts2拦截器配置
2011-03-21 11:11 1301Struts2框架的大部分功能都是通过拦截器来完成的。默认情况 ... -
数据校验DataValiation
2011-03-20 14:46 909struts.xml <?xml version=&qu ... -
JAVA Calendar详解
2011-03-20 14:00 816(在文章的最后,将会介绍Date类,如果有兴趣,可以直接翻到最 ... -
struts tags小结
2011-03-20 10:47 1603struts.xml <?xml version=&q ... -
ongl表达式用法
2011-03-19 21:39 1187struts.xml <?xml version=&qu ... -
action向其他页面传参数
2011-03-19 16:24 1415struts.xml <?xml version=&qu ... -
动态结果集DynamicResult
2011-03-19 16:04 920struts.xml <?xml version=&qu ... -
全局结果集 global-results | extends
2011-03-19 15:53 909struts.xml <?xml version=&qu ...
相关推荐
Struts 是一个开源的 JavaEE ...以上是对Struts开发模式的经验总结,涵盖了从环境配置到实际开发的关键点。在实际开发中,还需要对异常处理、国际化、安全性等方面有深入理解,以构建稳定、高效、可扩展的Struts应用。
总结来说,Struts框架是J2EE Web开发的重要工具,它以MVC设计模式为基础,通过提供标准化的组件和控制器,简化了开发流程,提高了开发效率,同时保持了代码的可维护性和可扩展性。学习和掌握Struts对于Java Web...
**问题描述**:在使用Struts2开发的应用程序中,如果连接的数据库账户权限不足,可能会导致查询、插入等操作失败。 **解决方案**:通过SQL语句为特定用户授予必要的权限。 1. **示例SQL语句**: ```sql GRANT ...
通过以上对Struts2的总结,我们可以看到,它在Java Web开发中的作用不可忽视。然而,随着Spring Boot和其他现代框架的发展,Struts2的使用逐渐减少,但在理解MVC模式和企业级应用开发方面,Struts2仍然具有重要的...
### Struts2学习小结 #### 一、Struts2简介及环境搭建 **Struts2** 是一款基于 **MVC**(Model-View-Controller)设计模式的开源框架,用于简化 Java Web 应用程序的开发过程。通过将业务逻辑、用户界面以及控制...
总结起来,Struts 1.x的三种开发方法各有优缺点。全手工适合于对框架有深厚理解且喜欢精细控制的开发者;半手工半工具适合希望提高开发速度但又不想完全依赖工具的团队;全工具自动化则适用于快速原型开发和初学者,...
在这个“Struts2项目开发经验总结”中,我们将深入探讨Struts2的核心概念、关键特性以及在实际项目中的应用。 首先,Struts2作为控制器,负责处理HTTP请求,并通过Action类来执行业务逻辑。Action类是Struts2的核心...
### Struts2开发环境的搭建 #### 一、概述 在Java Web开发领域,Struts2框架因其灵活性和强大的功能而备受青睐。本文将详细介绍如何搭建Struts2开发环境,并提供三种不同的方法来帮助读者快速上手。无论是初学者...
总结起来,Struts开发项目的基本架构涉及了多个组件和步骤,它们协同工作以实现高效的MVC模式。通过理解并熟练运用这些组件,开发者可以构建出健壮、可扩展的Web应用。不过,随着技术的发展,Struts已经被Spring MVC...
总结一下,搭建Struts开发环境主要包括以下步骤: 1. 安装JDK并配置环境变量。 2. 下载并安装Eclipse,创建Java项目。 3. 引入Hibernate,配置数据库连接。 4. 安装Apache Struts,配置核心文件`struts.xml`。 5. ...
总结来说,这个"学生管理的Struts框架开发实例"是一个很好的起点,它帮助初学者理解Struts如何组织应用程序,如何通过Action处理业务逻辑,如何使用ActionForm传递数据,以及如何通过ActionMapping和...
本笔记将全面总结Struts2的核心概念、主要功能以及实际开发中的应用。 一、Struts2概述 Struts2是Apache软件基金会下的一个开源项目,它继承了Struts1的优点并解决了其存在的问题,如性能和灵活性。Struts2的核心是...
原文提到的“io svn,cvs”可能是指在开发Struts2项目时所使用的版本控制系统。SVN(Subversion)和CVS(Concurrent Versions System)都是常用的版本控制工具,它们可以帮助团队协作开发时有效地管理代码版本。在...