struts2框架搭建

个人学习总结:

开发顺序:

1、界面原型设计

2、配置struts.xml文件

3、建立数据库

4、建立Model层

5、建立Service层

6、着手开发

7、测试


1、界面原型设计

主要是根据具体业务确定所需要的界面(jsp文件),下一步根据jsp的文件名称配置struts.xml文件中的Action。


2、配置struts.xml文件

主要工作包括:

2.1 配置默认访问的Action,界面入口Action,具体界面业务的Action。

2.2 同时建立Action对应的Class类和Model层的雏形。

2.3 最后要测试完成整个界面业务的畅通,在测试的过程中逐步完善struts.xml文件的配置以及相关工作。


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 -->
		<action name="index" class="com.bbs2014.action.CategoryAction" method="list">
			<result>/admin/index.html</result>
		</action>

		<!-- 具体界面业务Action -->
		<!-- 两result可以再进行简化,但是struts2中提供了result的返回值为INPUT的类型专门用来输入界面使用 -->
		<action name="*_*" class="com.bbs2014.action.{1}Action" method="{2}">
			<result>/admin/{1}_{2}.jsp</result>
			<result name="input">/admin/{1}_{2}.jsp</result>
		</action>
	</package>

	<!-- 默认访问Action -->
	<package name="front" namespace="/" extends="struts-default">

		<default-action-ref name="Category_list" />
		<action name="Category_list" class="com.bbs2014.action.CategoryAction" method="list">
			<result>/index.jsp</result>
		</action>
	</package>

</struts>


Action对应的Class类

package com.bbs2014.action;


import java.util.List;


import com.bbs2014.model.Category;
import com.opensymphony.xwork2.ActionSupport;


public class CategoryAction extends ActionSupport{
private List<Category> categories;
public String list(){
return SUCCESS;
}
public String add(){
return SUCCESS;
}
public String update(){
return SUCCESS;
}
public String delete(){
return SUCCESS;
}
public String addInput(){
return INPUT;
}
public String updateInput(){
return INPUT;
}

}

Model层的雏形

package com.bbs2014.model;

public class Category {

}

到这一步为止,struts.xml配置结束,在测试过程中要确保界面业务畅通,再进行下一步开发。


3、建立数据库


利用SQL语句创建数据库,创建表格,同时可将SQL语句保存在工程当中。

CREATE DATABASE bbs2014;
USE bbs2014;
CREATE TABLE _category(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(50),description VARCHAR(200));


4、建立Model层


Model层的设计参考数据库表中的字段,定义变量并自动生成get和set方法。

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


5、建立Service层


5.1.  创建一个工具包封装连接数据的类

5.2.  创建一个Service包,创建Service类,

5.3.  创建各种方法,(增删改查)

5.4.  实现创建的各种方法(连上数据库,创建sql语句)


连接数据库的工具类

package com.bbs2014.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:3306/bbs2014", "root", "liuyunlong");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	public static PreparedStatement prepared(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){
		if (conn == null) {
			return;
		}
		try {
			conn.close();
			conn = null;
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void close(Statement ps){
		if (ps == null) {
			return;
		}
		try {
			ps.close();
			ps = null;
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void close(ResultSet rs){
		if (rs == null) {
			return;
		}
		try {
			rs.close();
			rs = null;
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

创建Service类,创建各种方法并实现

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

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



6、着手开发

该步骤主要工作就是将界面,Model和Service层联系起来。

6.1 在Action对应的类的各种方法中,调用Service类中的方法,并生成变量的get和set方法(还没理解透)

6.2 测试对数据库的操作是否正常,边测试边完善界面的设计。

其中更新功能的实现是给一个id先从数据库中查出当前的category并显示(CategoryService中的loadById方法),之后再Category_updateInput.jsp中将更新后的新的category的值赋给当前的category,在通过CategoryService中的update()方法更新数据库中的值。


CategoryAction中调用CategoryService中对应的方法

package com.bbs2014.action;

import java.util.List;

import com.bbs2014.model.Category;
import com.bbs2014.service.CategoryService;
import com.opensymphony.xwork2.ActionSupport;

public class CategoryAction extends ActionSupport{
	private List<Category> categories;
	private Category category;
	private int id;
	CategoryService categoryService = new CategoryService();
	public String list(){
		categories = categoryService.list();//给categories赋值
		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(){
		category = categoryService.loadById(id);
		return INPUT;
		
	}
	public List<Category> getCategories() {
		return categories;
	}
	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}
	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;
	}
	public CategoryService getCategoryService() {
		return categoryService;
	}
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

}



Category_list.jsp

  <body>
  Category_list
  <br>
<a href="admin/Category_addInput">添加Category</a>

<hr/>
<s:iterator value="categories" var="c">
	name=<s:property value="#c.name"/> ,
	description=<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>
Category_addInput.jsp

  <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>
Category_updateInput.jsp

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


7、测试
最后在界面上测试所有功能是否都能实现(增删改查)






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值