struts2框架模板,商品列表,json-default视频笔记,easyui实现管理模块【诗书画唱】
CTRL+F:视频笔记和视频例子。讲义。json-default和struts-default区别的例子。作业和自己给出的答案。文章链接。框架的html模板。
讲义 START

管理模块:
struts2+easyui+mysql
ui:界面,ui框架专门用来设计界面
easyui,bootstrap,layui,elementui
datagrid:数据表格,table
讲义 END
代码例子 START
json-default和struts-default区别的例子 START

在deploy后,启动tomcat时却报了There is no result type defined for type ‘json’ mapped with name ‘success’. Did you mean ‘json’?的错误,因为struts2找不到json这个result type的定义。解决方法有下面两种:
1.将当前package的extends属性改为”json-default”,即让当前package从josn-default继承而不是struts-default继承;
2.但如果当前package确实无法继承”json-default”的话,还可以在当前package中定义result-type,将json给加进去

文章链接:https://blog.csdn.net/JavaMoo/article/details/71411543
json-default和struts-default区别的例子 END

select* from game
create table game(
id int primary key auto_increment,
gname varchar(100),
gtype varchar(100),
Gcomp varchar(100),
gyear varchar(100)
);
drop table game
insert into game(gname,
gtype,
Gcomp,
gyear ) values("游戏名1","游戏类型1","游戏公司1","游戏发行时间1");
insert into game(gname,
gtype,
Gcomp,
gyear ) values("游戏名2","游戏类型2","游戏公司2","游戏发行时间2");
insert into game(gname,
gtype,
Gcomp,
gyear ) values("游戏名3","游戏类型3","游戏公司3","游戏发行时间3");


package com.jy.action;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.jy.bean.Game;
import com.jy.dao.GameDao;
public class GameAction {
// private InputStream ins;
// public InputStream getIns() {
// return ins;
// }
// public void setIns(InputStream ins) {
// this.ins = ins;
// }
// public String loadData() throws UnsupportedEncodingException{
// String jsonStr = "{\"total\":2,\"rows\":[{\"id\":1,\"gname\":\"王者荣耀\",\"gtype\":\"塔防\",\"Gcomp\":\"TENCENT\",\"gyear\":2010}]}";
// ins = new ByteArrayInputStream(jsonStr.getBytes("utf-8"));
// return "success";
// }
private Map<String,Object>map = new HashMap<String,Object>();
private GameDao gameDao = new GameDao();
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public String loadAll(){
List<Game>list = gameDao.selectAll();
map.put("rows", list);
map.put("total", list.size());
return "success";
}
}


package com.jy.bean;
public class Game {
private Integer id;
private String gname;
private String gtype;
private String Gcomp;
private String gyear;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public String getGtype() {
return gtype;
}
public void setGtype(String gtype) {
this.gtype = gtype;
}
public String getGcomp() {
return Gcomp;
}
public void setGcomp(String Gcomp) {
this.Gcomp = Gcomp;
}
public String getGyear() {
return gyear;
}
public void setGyear(String gyear) {
this.gyear = gyear;
}
}


package com.jy.dao;
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.jy.bean.Game;
import com.jy.util.DbUtil;
public class GameDao {
public List<Game>selectAll(){
String sql = "select * from game";
List<Game>list = new ArrayList<Game>();
Connection conn = null;
PreparedStatement pstm = null;
ResultSet rs = null;
try {
conn = DbUtil.getConn();
pstm = conn.prepareStatement(sql);
rs = pstm .executeQuery();
while(rs.next()){
Game g = new Game();
g.setId(rs.getInt("id"));
g.setGname(rs.getString("gname"));
g.setGtype(rs.getString("gtype"));
g.setGcomp(rs.getString("Gcomp"));
g.setGyear(rs.getString("gyear"));
list.add(g);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DbUtil.close(rs, pstm, conn);
}
return list;
}
}


package com.jy.util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
public class DbUtil {
private static String driverName;
private static String url;
private static String user;
private static String pwd;
static {
//读取properties文件
Properties prop = new Properties();
//将db.properties文件读取到内存中去
InputStream is = DbUtil.class.getClassLoader()
.getResourceAsStream("db.properties");
//加载内容
try {
prop.load(is);
//读取内容
driverName = prop.getProperty("drivername");
url = prop.getProperty("url");
user = prop.getProperty("username");
pwd = prop.getProperty("password");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//获取数据库连接对象的方法
public static Connection getConn(){
Connection conn = null;
try {
Class.forName(driverName);
conn = DriverManager.getConnection(url,user,pwd);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void close(ResultSet rs,PreparedStatement pstm
,Connection conn){
try {
if(rs != null) {
rs.close();
}
if(pstm != null) {
pstm.close();
}
if(conn != null) {
conn.close();
}
} catch(Exception e) {
e.printStackTrace();
}
}
}


drivername=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/firstjsp?useUnicode=true&characterEncoding=GBK2312
username=root
password=root

<?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="my" namespace="/gm" extends="json-default">
<action name="ldAc" class="com.jy.action.GameAction"
method="loadData">
<result type="stream">
<param name="contentType">text/plain</param>
<!-- action返回的字符串的内容取自GameAction的哪个属性 -->
<param name="inputName">ins</param>
</result>
</action>
<action name="ldAllAc" class="com.jy.action.GameAction"
method="loadAll">
<result type="json">
<param name="root">map</param>
<param name="contentType">text/html</param>
</result>
</action>
</package>
</struts>

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>J190802</display-name>
<!-- struts2框架的配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

{"total":2,"rows":[{"id":1,"gname":"WANGZHERONGYI","gtype":"MOB","Gcomp":"TENCENT","gyear":2010}]}

<%@ page language="java" contentType="text/html; charset=UTF-8" 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 hreff="<%=basePath%>">
<title></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" hreff="css/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" hreff="css/themes/icon.css">
<script type="text/javascript" srcc="js/jquery-1.11.0.js"></script>
<script type="text/javascript" srcc="js/jquery.easyui.min.js"></script>
<!-- 语言汉化包 -->
<script type="text/javascript" srcc="js/locale/easyui-lang-zh_CN.js"></script>
</head>
<body>
<table class="easyui-datagrid" title="游戏管理模块"
style="width:1000px;height:450px"
data-options="singleSelect:true,collapsible:true,
url:'gm/ldAllAc.action',method:'get',pagination:true">
<thead>
<tr>
<th data-options="field:'id',width:80">游戏ID</th>
<th data-options="field:'gname',width:100">游戏名称</th>
<th data-options="field:'gtype',width:80,align:'right'">游戏类型</th>
<th data-options="field:'Gcomp',width:80,align:'right'">游戏公司</th>
<th data-options="field:'gyear',width:250">游戏年份</th>
</tr>
</thead>
</table>
</body>
</html>

运行结果:

代码例子 END
视频笔记和视频例子 START



框架的html模板:


视频笔记和视频例子 END
作业和自己给出的答案 START
使用easyui+struts2+mysql实现加载商品数据功能,参照课堂实例

package com.jy.action;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.jy.bean.Product;
import com.jy.dao.ProductDao;
public class ProductAction {
private Map<String,Object>map = new HashMap<String,Object>();
private ProductDao ProductDao = new ProductDao();
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public String loadAll(){
List<Product>list = ProductDao.selectAllProduct();
map.put("rows", list);
map.put("total", list.size());
return "success";
}
}


package com.jy.bean;
public class Product {
/*pid int primary key auto_increment,
pname varchar(100),
price double*/
private Integer pid;
private String pname;
private Double price;
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}

<?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="my" namespace="/gm" extends="json-default">
<!-- 游戏表的配置START -->
<action name="ldAc" class="com.jy.action.GameAction"
method="loadData">
<result type="stream">
<param name="contentType">text/plain</param>
<!-- action返回的字符串的内容取自GameAction的哪个属性 -->
<param name="inputName">ins</param>
</result>
</action>
<action name="ldAllAc" class="com.jy.action.GameAction"
method="loadAll">
<result type="json">
<param name="root">map</param>
<param name="contentType">text/html</param>
</result>
</action>
<!--游戏表的配置 END -->
<!--商品表的配置START -->
<action name="ldAc" class="com.jy.action.ProductAction"
method="loadData">
<result type="stream">
<param name="contentType">text/plain</param>
<!-- action返回的字符串的内容取自GameAction的哪个属性 -->
<param name="inputName">ins</param>
</result>
</action>
<action name="ProductActionName" class="com.jy.action.ProductAction"
method="loadAll">
<result type="json">
<param name="root">map</param>
<param name="contentType">text/html</param>
</result>
</action>
<!--商品表的配置 END -->
</package>
</struts>


package com.jy.dao;
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.jy.bean.Product;
import com.jy.util.DbUtil;
public class ProductDao {
public List<Product>selectAllProduct(){
String sql = "select * from Product";
List<Product>list = new ArrayList<Product>();
Connection conn = null;
PreparedStatement pstm = null;
ResultSet rs = null;
try {
conn = DbUtil.getConn();
pstm = conn.prepareStatement(sql);
rs = pstm .executeQuery();
while(rs.next()){
Product g = new Product();
g.setPid(rs.getInt("Pid"));
g.setPname(rs.getString("Pname"));
g.setPrice(rs.getDouble("Price"));
list.add(g);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DbUtil.close(rs, pstm, conn);
}
return list;
}
}

<%@ page language="java" contentType="text/html; charset=UTF-8" 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 hreff="<%=basePath%>">
<title></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" hreff="css/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" hreff="css/themes/icon.css">
<script type="text/javascript" srcc="js/jquery-1.11.0.js"></script>
<script type="text/javascript" srcc="js/jquery.easyui.min.js"></script>
<!-- 语言汉化包 -->
<script type="text/javascript" srcc="js/locale/easyui-lang-zh_CN.js"></script>
</head>
<body>
<table class="easyui-datagrid" title="商品管理模块"
style="width:1000px;height:450px"
data-options="singleSelect:true,collapsible:true,
url:'gm/ProductActionName.action',method:'get',pagination:true">
<thead>
<tr>
<th data-options="field:'pid',width:80">商品ID</th>
<th data-options="field:'pname',width:100">商品名称</th>
<th data-options="field:'price',width:80,align:'right'">商品价格</th>
</tr>
</thead>
</table>
</body>
</html>
以上的width是可以设置这个宽度的,还有其他属性也可以进行数值的设置,凭自己的需求去设置数字。
