struts开发小结

<%@ 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));

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值