struts2.1.6 bbs 07

<default-action-ref name="Category_list"/>

http://localhost:8080/Struts2_30_BBS2011_02  默认执行的action


登陆 Login.jsp
注册 Register.jsp
查询用户信息 SelectUserInfo.jsp
添加用户信息 AddUserInfo.jsp
删除用户信息 DelUserInfo.jsp
修改用户信息 UpdateUserInfo.jsp
.......... 

登陆 Login
注册 Register
用户 User
创建 Create
修改 Update
删除 Delete
查询 Selete
控制器 Controller
用户名 Username
密码 Password

文件名自己组合,比如登陆页面Login.jsp,修改用户信息页面UpdateUser.jsp或Update.jsp,诸如此类


1.         读doc文档:struts-tags

2.         设计约定(编码规定)

a)         原则:简单就是美

b)         库名:项目名

c)         表的命名:_Model名

d)         字段:保持和属性名一致(尽量不要起名和数据库命名冲突)

e)         用层来划分包com.bjsxt.bbs.action model(bean) service dto(vo)

f)          Action XXXXAction

g)         *-*

h)         /

i)           /admin

j)           package “action” adminAction

项目开发顺序-以BBS2009的名义
1.         建立界面原型

2.         建立Struts.xml

a)         确定namespace

b)         确定package

c)         确定Action的名称,空的方法

d)         确定Result

e)         将界面原型页面进行修改,匹配现有设置

f)          测试

g)         做好规划!!!!!

3.         建立数据库(或者实体类)

4.         建立Model层

5.         建立Service层(后面讲了Hibernate后再完善)

a)         此时可以使用JUnit进行单元测试了

6.         着手开发



<default-action-ref name="index"/>

<action name="*_*" class="com.gz.bbs2011.action.{1}Action" method="{2}">
这两个不能一起用,有bug

package com.gz.bbs2011.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class DB {
	public static Connection createConnection() {
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost/bbs2011", "root", "root");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	public static PreparedStatement prepare(Connection conn, String sql) {
		PreparedStatement pstm = null;
		try {
			pstm = conn.prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pstm;
	}
	public static void close(Connection conn) {
		try {
			conn.close();
			conn = null; //设为空 垃圾收集器马上可以回收
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static void close(PreparedStatement pstm) {
		try {
			pstm.close();
			pstm = null;
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static void close(ResultSet rs) {
		try {
			rs.close();
			rs = null;
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

package com.gz.bbs2011.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;
	}
	
}

package com.gz.bbs2011.action;
import java.util.List;
import com.gz.bbs2011.model.Category;
import com.gz.bbs2011.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() {
		categories = categoryService.list();
		return SUCCESS; 
	}
	public String add() {
		categoryService.add(category);
		return SUCCESS;
	}
	public String delete() {
		categoryService.deleteById(id);   
//		categoryService.delete(category);   
		return SUCCESS;
	}
	public String update() {
		categoryService.update(category);
		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;
	}
	
}

package com.gz.bbs2011.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.gz.bbs2011.model.Category;
import com.gz.bbs2011.util.DB;

public class CategoryService {
	public void add(Category c) {
		Connection conn = DB.createConnection();
		String sql = "insert into _category values(null,?,?)";
		PreparedStatement pstm = null;
		try {
			pstm = DB.prepare(conn, sql);
			pstm.setString(1, c.getName());
			pstm.setString(2, c.getDescription());
			pstm.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DB.close(conn);
		DB.close(pstm);
	}
	public List<Category> list() {
		Connection conn = DB.createConnection();
		String sql = "select * from _category";
		PreparedStatement pstm = DB.prepare(conn, sql);
		List<Category> list = new ArrayList<Category>();
		try {
			ResultSet rs = pstm.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"));
				list.add(c);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DB.close(pstm);
		DB.close(conn);
		return list;
	}
	public void delete(Category c) {
		deleteById(c.getId());
	}
	public void deleteById(int id) {
		Connection conn = DB.createConnection();
		String sql = "delete from _category where id=?";
		PreparedStatement pstm = null;
		try {
			pstm = DB.prepare(conn, sql);
			pstm.setInt(1, id);
			pstm.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DB.close(pstm);
		DB.close(conn);
		
	}
	
	public void update(Category c) {
		Connection conn = DB.createConnection();
		String sql = "update _category set name=?,description=? where id=?";
		PreparedStatement pstm = null;
		try {
			pstm = DB.prepare(conn, sql);
			pstm.setString(1, c.getName());
			pstm.setString(2, c.getDescription());
			pstm.setInt(3, c.getId());
			pstm.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DB.close(pstm);
		DB.close(pstm);
	}
	public Category loadById(int id) {
		Connection conn = DB.createConnection();
		String sql = "select * from _category where id=?";
		PreparedStatement pstm = DB.prepare(conn, sql);
		ResultSet rs = null;
		Category c = null;
		try {
			pstm.setInt(1, id);
			rs = pstm.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(pstm);
		DB.close(conn);
		return c;
	}
}

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
		<!-- 
	
	<package name="bbs2009_default" extends="struts-default">
		 <global-exception-mappings>
    		<exception-mapping result="exception_handle" exception="Exception"></exception-mapping>
    	</global-exception-mappings>
	</package>
	   --> 
	   
	   
    <package name="admin" namespace="/admin" extends="struts-default" >
    	

    	<action name="index">
    		<result>/admin/index.html</result>
    	</action>
    	
    
       <action name="*_*" class="com.gz.bbs2011.action.{1}Action" method="{2}">
       		<result>/admin/{1}_{2}.jsp</result>
       		<result name="input">/admin/{1}_{2}.jsp</result>
       </action>
       <!-- 
       <action name="category" class="com.gz.bbs2011.action.{1}Action">
       	<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" >
       <action name="Category_list" class="com.gz.bbs2011.action.CategoryAction" method="list">
       		<result>/admin/Category_list.jsp</result>
       </action>
    </package>
    

</struts>


<body>
	Category_list
	<a href="admin/Category_addInput">添加Category</a>
  
	<hr/>
	<s:iterator value="categories" var="c">
<table border="1" width="30%">
	<tr>
		<td><s:property value="#c.name"/></td>
		<td><s:property value="#c.description"/></td>
		<td><a href="admin/Category_delete?id=<s:property value="#c.id"/>">删除Category</a></td>
		<td><a href="admin/Category_updateInput?id=<s:property value="#c.id"/>">更新Category</a></td>
	</tr>
</table>
	
	</s:iterator>
	<s:debug></s:debug>
  </body>

<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>

<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>

你可能感兴趣的:(sql,struts,String,input,action,bbs)