struts2-剩余2

周一-周四   晚上:7:30 – 9:30

多线程、Linux、云服务器、git

spring

springboot、springcloud

今晚:7:30  测试直播网速

一、回顾

拦截器:在目标action方法执行的前后插入一段额外的逻辑代码;aop面向切面编程的一种实现

实现Interceptor接口,intercept()核心方法,每次调用目标方法时都会被执行

struts.xml配置:

package:<interceptors>

action:<interceptor-ref name/>

package默认继承struts-defalut

i18n

二、log4j

以前我们的代码有bug,使用system.out.println()语句来输出打印

有时我们需要对我们自己的代码运行结果进行记录,可以使用log4j

log4j是Apache的一个开源项目,可以控制日志信息输出的位置(控制台,文件,数据库)

日志七个级别

  由低到高分别是:OFF ,DEBUG,INFO,WARN,ERROR,FETAL,ALL

官方建议使用红色

使用,

  添加log4j的jar包,然后再src目录配置log4j.properties

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
# 设置 日志级别和appender
log4j.rootLogger = info,stdout,logFile # 输出信息到控制抬
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n # 输出dubug级别以上的日志到文件
log4j.appender.logFile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.logFile.File = E://logs/log.log
log4j.appender.logFile.Append = true
log4j.appender.logFile.Threshold = DEBUG
log4j.appender.logFile.layout = org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
public class Demo1Action extends ActionSupport {

    private String name;
//日志记录器
private Logger log = Logger.getLogger(Demo1Action.class); @Override
public String execute() throws Exception { log.debug("dubug......");
log.info("info......");
log.warn("warn......");
log.error("error......"); return SUCCESS;
} }

三、ajax访问action

(1)demo2.jsp

struts2-剩余2

(2)Demo2Action

@Getter
@Setter
public class Demo2Action extends ActionSupport { private String uname;
private String pname;
//此方法通过前端ajax调用,所以不需要返回值,返回的数据通过response响应
public void m1() throws IOException{
System.out.println("姓名:"+uname+",密码:"+pname);
//获取response响应对象
HttpServletResponse response = ServletActionContext.getResponse();
//设置类型和编码
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
if("admin".equals(uname) && "123456".equals(pname)){
response.getWriter().write("1"); //成功
}else{
response.getWriter().write("0"); //失败
}
}
}

(3)struts.xml

<action name="demo2Action" class="com.yujun.maven.action.Demo2Action">
</action>

四、highcharts图表-折线图

(1)City

@Setter
@Getter
public class City {
private String name; //城市名
private double[] data; //12月份的温度数组
}

(2)Demo3Action

public class Demo3Action extends ActionSupport {

    public void m1() throws Exception {

        //模拟数据库,创建数据
City cq = new City();
cq.setName("重庆");
cq.setData(new double[]{7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2,26.5, 23.3, 18.3, 13.9, 9.6}); City lz = new City();
lz.setName("兰州");
lz.setData(new double[]{-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8,24.1, 20.1, 14.1, 8.6, 2.5}); //集合
List<City> list = new ArrayList<City>();
list.add(cq);
list.add(lz); //把集合转为json数据
ObjectMapper map = new ObjectMapper();
String json = map.writeValueAsString(list); //把json响应到前端
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(json);
}
}

(3)struts.xml

<action name="demo3Action" class="com.yujun.maven.action.Demo3Action">
</action>

(4)demo3.jsp

<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="js/highcharts.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var title = {
text: '城市平均气温'
};
var subtitle = {
text: 'Source: runoob.com'
};
var xAxis = {
categories: ['一', '二', '三', '四', '五', '六',
'七', '八', '九', '十', '十一', '十二']
};
var yAxis = {
title: {
text: 'Temperature (\xB0C)'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
}; var tooltip = {
valueSuffix: '\xB0C'
} var legend = {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
}; var series; //ajax请求后台action获取json数据
$.ajax({
async:false,
dataType:'json',
type:'get',
url:'demo3Action!m1.action',
success:function(data){
series = data;
}
}) var json = {}; json.title = title;
json.subtitle = subtitle;
json.xAxis = xAxis;
json.yAxis = yAxis;
json.tooltip = tooltip;
json.legend = legend;
json.series = series; $('#container').highcharts(json);
});
</script>
</head>
<body>
<div id="container" style="width: 550px; height: 400px; margin: 0 auto"></div>
</body>
</html>

Jackson依赖

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>

更多图表案例,请自行参照菜鸟教程

三件事:

(1)19:30直播平台测试

(2)单表使用struts2框架增删改查(可以没有分页和查询条件)

(3)预习hibernate第一章

上一篇:OpenTSDB案例总结


下一篇:kvm编译安装及常见问题解决