技术标签: Java
实现一个简单的列表功能
创建项目导入相关的包:
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.8</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.8</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
导入目标服务器运行环境 Tomcat Runtime
配置Struts2 和 Spring: web.xml
<filter>
<display-name>StrutsPrepareAndExecuteFilter</display-name>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
添加 Spring 和 Struts2 的配置文件
spring-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<context:component-scan
base-package="cn.tedu.controller"/>
<context:component-scan
base-package="cn.tedu.service"/>
</beans>
struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
</struts>
创建业务层:
@Service
public class ProductService {
List<Product> products;
@PostConstruct
private void init(){
products = new ArrayList<Product>();
products.add(new Product(
1,"大黄蜂",24.5,"变形金刚经典系列"));
products.add(new Product(
2,"擎天柱",88.5,"变形金刚经典系列"));
products.add(new Product(
3,"光头强",12.5,"大笨熊你粗来!"));
products.add(new Product(
4,"李老师",22.5,"胡说派诗人!"));
products.add(new Product(
5,"范传奇",23.5,"变形金刚消费者!"));
}
public List<Product> getProducts(){
return products;
}
}
创建控制器
@Controller
@Scope("prototype")
public class ProductAction {
@Resource
private ProductService productService;
private List<Product> products;
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//控制器方法
public String list(){
name = "list";
products = productService.getProducts();
ActionContext context =
ActionContext.getContext();
context.getSession().put(
"loginUser", "老王");
return "success";
}
}
创建 list.jsp
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>产品管理</title>
<style type="text/css">
table {
border-collapse: collapse;
}
td, th{
padding: 5px 10px;
}
th{
border-bottom: 2px solid #ddd;
}
td{
border-top: 1px solid #ccc;
}
</style>
</head>
<body>
<h1>产品管理</h1>
<table>
<thead>
<tr>
<th>编号</th>
<th>名称</th>
<th>价格</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<c:forEach items="${products}" var="p">
<tr>
<td>${p.id}</td>
<td>${p.name}</td>
<td>${p.price}</td>
<td>${p.description}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
配置struts.xml
<package name="product"
namespace="/product"
extends="struts-default">
<action name="list" class="productAction"
method="list">
<result>
/WEB-INF/jsp/list.jsp
</result>
</action>
</package>
测试:
http://localhost:8080/ssh3/product/list.action
Struts 中共享数据的一个机制, 利用ValueStack可以在Struts组件之间共享数据:
Struts组件包括:
JSP 中使用 s:debug 标签可以显示 ValueStack 对象
案例:
创建vs.jsp
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>产品管理</title>
<style type="text/css">
table {
border-collapse: collapse;
}
td, th{
padding: 5px 10px;
}
th{
border-bottom: 2px solid #ddd;
}
td{
border-top: 1px solid #ccc;
}
</style>
</head>
<body>
<h2>ValueStack</h2>
<s:debug></s:debug>
<h2>利用OGNL表的式读取ValueStack中的数据</h2>
<p>读取ValueStack中的Action对象的数据</p>
<s:property value="products[0].name"/>
<h3>利用ValueStack 和 OGNL 显示商品列表</h3>
<table>
<thead>
<tr>
<th>ID</th>
<th>名称</th>
<th>价格</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<s:iterator value="products" var="p">
<tr>
<td><s:property value="id"/></td>
<td><s:property value="name"/></td>
<td><s:property value="price"/></td>
<td><s:property value="description"/></td>
</tr>
</s:iterator>
</tbody>
</table>
</body>
</html>
配置struts.xml
<action name="vs" class="productAction"
method="list">
<result>
/WEB-INF/jsp/vs.jsp
</result>
</action>
重用控制器 productAction
测试:
http://localhost:8080/ssh3/product/vs.action
说明使用 ValueStack 和 OGNL 也可以显示产品列表.
JSP 中 可以使用 Struts 标签配合 OGNL 表达式读取 ValueStack中的数据, 由于Action对象被放到了 ValueStack中这样就可以, Action 到 JSP共享数据了.
关于 ValueStack 和 OGNL
<s:property value="#session.loginUser"/>
案例:
编写控制器
/**
* 利用 ValueStack 共享数据
*/
@Controller
@Scope("prototype")
public class DataAction extends AbstractAction{
//在内容区域共享数据, 利用控制器的Bean属性
private String name;
private int age;
//@Value("#{cfg.data}")
private String target; //目标网页名
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public String execute(){
//上下文区域(环境) Context 区域, 共享数据
request.put("message", "Struts 测试");
session.put("loginState", "已经登录");
application.put("count", 55);
name="老王";
age = 123;
target = "show.jsp";//目标视图页面
return SUCCESS;
}
}
编写 show.jsp
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ValueStack</title>
</head>
<body>
<h1>ValueStack</h1>
<h2>读取内容区域的值</h2>
<s:property value="name"/>
<s:property value="age"/>
<s:property value="target"/>
<h2>读取上下文区域的值</h2>
<p>读取上下文区域使用#为开头的表达式</p>
<s:property value="#request.message"/>
<s:property value="#session.loginState"/>
<s:property value="#application.count"/>
</body>
</html>
配置 struts.xml
<!-- 利用 OGNL 读取ValueStack中的数据 -->
<action name="data" class="dataAction">
<result>
/WEB-INF/jsp/${target}
</result>
</action>
表达式${target} 就是利用ValueStack从控制器读取了属性值.
测试
http://localhost:8080/ssh3/product/data.action
Struts : 架子
Spring : 春天
Hibernate 冬眠: 把对象冬眠一样持久的保存到数据库中.
核心想法: java程序员按照面向对象的方式操作数据库, 不再需要使用SQL.
Hibernate 的问题
解决了 ORM 问题: 对象关系映射问题
导入包:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.9.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
配置Hibernate主配置文件 hibernate.cfg.xml:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/cloud_note
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
主配置文件主要是提供 数据库 连接参数和MySQL方言
注意: 面试时候经常有人问方言!!!
show_sql 和 format_sql 属性用于调试阶段, 在控制台显示Hibernate生成并且执行的SQL语句.
创建表:
create table p_person(
id int not null AUTO_INCREMENT,
name varchar(100),
primary key(id)
);
insert into p_person (name) values ('李老师');
insert into p_person (id, name) values (null,'李老师');
创建实体类 Person
public class Person implements Serializable{
private Integer id;
private String name;
public Person() {
}
public Person(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
编写映射文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Hibernate 映射文件负责:
映射 实体和表的对应关系, 映射实体属性和列的对应关系
Class Person{ p_person(
id id
name name
} )
-->
<hibernate-mapping>
<class name="cn.tedu.entity.Person"
table="p_person">
<!-- id 属性是主键, 使用id标签映射 -->
<id name="id" column="id">
<!-- class=identity 用于处理自增类型 -->
<generator class="identity"/>
</id>
<!-- 映射普通属性
Person.name -> p_person.name -->
<property name="name" column="name"/>
</class>
</hibernate-mapping>
Hibernate 映射文件负责: 映射 实体和表的对应关系, 映射实体属性和列的对应关系
编写测试案例:
public class HibernateTest {
@Test
public void testSavePerson(){
//参考: Hibernate 官方手册!
//读取配置文件
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
//创建Session工厂
SessionFactory factory =
cfg.buildSessionFactory();
//利用Session工厂创建Session
Session session=factory.openSession();
//session 自动提供了对象的 CRUD 操作方法
//从数据库中查询id为1的person对象
Person p =
(Person)session.get(Person.class,1);
System.out.println(p);
//最后务必关闭 session
session.close();
}
}
测试…
重构测试案例:
public class HibernateTest {
SessionFactory factory;
Session session;
@Before
public void init(){
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
factory = cfg.buildSessionFactory();
session = factory.openSession();
}
@After
public void destory(){
session.close();
factory.close();
}
@Test
public void testGet(){
Person p = (Person)session.get(
Person.class, 7);
System.out.println(p);
}
@Test
public void testAdd(){
//将新对象保存到数据库中, 需要开启事务
Transaction tx=session.beginTransaction();
Person one = new Person(null, "熊大");
session.save(one);
tx.commit();//提交事务
System.out.println(one);
}
@Test
public void testUpdate(){
//更新数据
Transaction tx = session.beginTransaction();
Person p = (Person)session.get(
Person.class, 7);
p.setName("范传奇");
session.update(p);
tx.commit();
}
@Test
public void testDelete(){
//更新数据
Transaction tx = session.beginTransaction();
Person p = (Person)session.get(
Person.class, 7);
session.delete(p);
tx.commit();
}
@Test
public void testSavePerson(){
//参考: Hibernate 官方手册!
//读取配置文件
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
//创建Session工厂
SessionFactory factory =
cfg.buildSessionFactory();
//利用Session工厂创建Session
Session session=factory.openSession();
//session 自动提供了对象的 CRUD 操作方法
//从数据库中查询id为1的person对象
Person p =
(Person)session.get(Person.class,1);
System.out.println(p);
//最后务必关闭 session
session.close();
}
}
测试
Spring 提供了整合Hibernate功能, 使Hibernate的使用更加简洁方便:
配置使用:
导入包:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
添加Spring配置文件 spring-orm.xml
配置数据源, 用于连接数据库:
<!-- 1. 配置数据库连接池 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/cloud_note"/>
<property name="username"
value="root"/>
<property name="password"
value="root"/>
</bean>
配置SessionFactory
<!-- 2. 配置Session工厂-->
<!-- Spring orm提供了一个工厂Bean, 用于管理Hibernate
的 Session 工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"
ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>classpath:Person.hbm.xml</value>
</list>
</property>
</bean>
配置 HibernateTemplate
<!-- 3. 配置HibernateTemplate -->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory"
ref="sessionFactory"/>
</bean>
测试:
public class SpringHibernateTest {
ClassPathXmlApplicationContext ctx;
HibernateTemplate temp;
@Before
public void init(){
ctx = new ClassPathXmlApplicationContext(
"spring-orm.xml");
temp = ctx.getBean(
"hibernateTemplate",
HibernateTemplate.class);
}
@After
public void destroy(){
ctx.close();
}
@Test
public void testGet(){
//HibernateTemplate 可以替代 session
//并且使用更加简便(可以不用管理事务,等)
Person p = temp.get(Person.class, 1);
System.out.println(p);
}
}
1.关于setC++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构操作。vector封装数组,list封装了链表,map和set封装了二叉树等,在封装这些数据结构的时候,STL按照程序员的使用习惯,以成员函数方式提供的常用操作,如:插入、排序、删除、查找等。让
谈到网站seo优化,就会涉及到“程序代码”优化、网站架构优化、网站内容优化、网站链接优化等,而对于很多seoer人员,在程序代码方面的优化能力较为薄弱,甚至有一些想学习从事seo行业的朋友,由于自己不懂程序代码、不懂开发等,而不敢尝试学习进入seo这个行业,非常担心自己学不会,搞不明。对于有这样思想包袱的朋友,小编今天大胆的告诉你,你的担心都是多余的,其实seo的技术难度并非大家想象的如此
上一篇分析之后,本来第二部分分析是想分析start之后的逻辑的,这样会让人比较快速的理解skynet框架。但想想还是顺着代码启动的思路写下去会比较好,我觉得这样我自己更容易理解。 bootstrap是引导程序的意思,在skynet中,的确也是做了服务器工作的前置任务。再skynet_start.c中//启动logger服务struct skynet_context *ctx = skynet_context_new(config->logservice, conf...
一个偶然的机会,有幸邀请到了一家国外专门做web安全的公司来对自己的web系统做安全测试。4周下来,我与几位安全专家多次沟通,完成了对自己系统的威胁建模,渗透测试,白盒测试,一共发现了28个漏洞。经验宝贵,因此有必要好好总结下。
文章目录题目APK静态分析jadx反编译IDA反汇编JEB动态调试工具的使用操作内存值AndroidKiller工具的使用篡改软件包总结题目攻防世界 Mobile 新手区题目链接 Ph0en1x-100,如下:下载附件得到一个 apk,安装后如下:要求输入一个 flag 值进行校验,很显然,需要逆向分析 APK 并获取 Flag。APK静态分析拖进查壳工具,未加壳:jadx反编译裸奔,当然是拖进 jadx 查看源码:关键看第 34 行的判断语句,它是通过比较 getSecret(get
前言:传统的javascript中只有对象,没有类的概念。它是基于原型的面向对象语言。原型对象特点就是将自身的属性共享给新对象。这样的写法相对于其它传统面向对象语言来讲,独树一帜也可以说难以接受!ES5中的类ES5中如果要生成一个对象实例,需要先定义一个构造函数,然后通过new操作符来完成。示例://构造函数名大写(非强制,但这么写有助于区分构造函数和普通函数)function Person(name,age) { this.name = name; this.age=age
介绍坐标变换矩阵是一个3*3的矩阵,用来对图形进行坐标变化,将原来的坐标点转移到新的坐标点,因为一个图片是有点阵和每一点上的颜色信息组成的,所以对坐标的变换,就是对每一点进行搬移形成新的图片。具体的说图形的放大缩小,移动,旋转,透视,扭曲这些效果都可以用此矩阵来完成。平移旋转绕原点逆时针旋转θ度角的变换公式是 x’ = xcosθ − ysinθ 与 y。’ = xsinθ + ycos
SecureCRT下的串口不能输入用secureCRT建了一个串口COM1后,连接上开发板后,可以正确接受和显示串口的输出,但是按键输入无效。解决方法:Session Options -> Connection -> Serial -> Flow Control,将原先默认选中的 RTS/CTS取消掉,再重新connect开发板,再次连上后,此时就可以从键盘输入了。
C语言文件操作为什么使用文件?什么是文件?程序文件数据文件文件的打开和关闭文件指针文件的操作方式文件的顺序读写文件的随机读写什么是文本文件和二进制文件文件读取结束的判定文件缓冲区为什么使用文件?在前面的结构体的学习中,我们写了通讯录的程序,当通讯录运行起来的时候,可以给通讯录中增加、删除数据,此时数据是存放在内存中,当程序退出的时候,通讯录中的数据自然就不存在了,等下次运行通讯录程序的时候,数据又得重新录入,如果使用这样的通讯录就很难受。我在想既然是通讯录就应该把信息记录下来,只有我们自己选择删除数据的
MySQL主从复制(Master-Slave)实践MySQL数据库自身提供的主从复制功能可以方便的实现数据的多处自动备份,实现数据库的拓展。多个数据备份不仅可以加强数据的安全性,通过实现读写分离还能进一步提升数据库的负载性能。下图就描述了一个多个数据库间主从复制与读写分离的模型(来源网络):在一主多从的数据库体系中,多个从服务器采用异步的方式更新主数据库的变化,业务服务器在执行写或者相关修改数据库...
首先下载源码。把他放在自己的文件里面。下载地址稍后放上。然后<iframeref="mainiframe"width="100%"height="500px" :src='../static/js/pdf/web/viewer.html?file=' + http://xxxx.pdf></iframe>我使用的vue所以直接在页面上把src写上了。前面是view.html的相对位置加上?file加上pdf的地址,(这个pdf的地址是存在服务器的pdf文件地址,你...