J2EE Web开发入门—通过action是以传统方式返回JSON数据

关键字:mavenm2eclipseJSONStruts2Log4j2tomcatjdk7Config Browser Plugin

Created by Bob 20131031

l 开发准备工作

一、开发所需工具及相关组件介绍

Eclipse IDE for Java EE Developers, 247 MB

http://www.eclipse.org/downloads/

apache-tomcat-7.0.42-windows

http://www.apache.org/dist/tomcat/tomcat-7/v7.0.42/bin/

Java SE Development Kit 7u45

http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html

apache-maven-3.1.0-bin

http://maven.apache.org/download.cgi

二、搭建开发环境参考文档

JDK安装及配置

http://xiaoxing598.diandian.com/post/2012-05-06/17031020

Maven安装及配置

http://blog.csdn.net/yuguiyang1990/article/details/8775046

安装m2eclipse插件

http://blog.csdn.net/yuguiyang1990/article/details/8796410

Tomcat安装及配置

http://jingyan.baidu.com/article/8065f87fcc0f182330249841.html

SVN安装及配置

http://www.tech-juice.org/2012/05/01/how-to-add-a-new-eclipse-project-to-an-svn-repository/

ConfigBrowser插件

http://struts.apache.org/release/2.2.x/docs/config-browser-plugin.html

l 第一个开发实践

通过Struts2返回第一个JSON对象的源码下载:

http://bobproject.googlecode.com/svn/tags/tag_ReportWebApp_source_v1.0

一、通过maven的pom.xml引入相关组件

1、struts2的dependency

http://mvnrepository.com/artifact/org.apache.struts/struts2-core/2.3.15.2

2、json的dependency

        <dependency>

            <groupId>net.sf.json-lib</groupId>

            <artifactId>json-lib</artifactId>

            <version>2.4</version>

            <classifier>jdk15</classifier>

        </dependency>

3、javax.servlet的dependency

        <dependency>

            <groupId>org.eclipse.jetty.orbit</groupId>

            <artifactId>javax.servlet</artifactId>

            <version>3.0.0.v201112011016</version>

        </dependency>

4、log4j2的dependency http://logging.apache.org/log4j/2.x/maven-artifacts.html

<dependency>

            <groupId>org.apache.logging.log4j</groupId>

            <artifactId>log4j-api</artifactId>

            <version>2.0-beta9</version>

        </dependency>

        <dependency>

            <groupId>org.apache.logging.log4j</groupId>

            <artifactId>log4j-core</artifactId>

            <version>2.0-beta9</version>

        </dependency>

5、ConfigBrowser插件

        <dependency>

            <groupId>org.apache.struts</groupId>

            <artifactId>struts2-config-browser-plugin</artifactId>

            <version>2.3.15.2</version>

        </dependency>

 

二、搭建struts2程序框架

1、创建ReportAction.java

创建public void doAction() throws IOException;

2、创建struts.xml配置文件

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <package name="default" extends="struts-default" namespace="/">

        <action name="testJSONFromActionByGeneral" class="com.tuoming.iqa.action.ReportAction"

            method="doAction">

        </action>

    </package>

</struts>

3、web.xml中添加struts2配置代码

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5"

    xmlns="http://java.sun.com/xml/ns/javaee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  <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>*.action</url-pattern>

  </filter-mapping>

  </web-app>

4、index.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 XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

<title>Struts2与JSON测试</title>

<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>

<script type="text/javascript" src="js/index.js"></script>

</head>

<body>

    <a href="<%=basePath%>testJSONFromActionByGeneral.action" title="测试从Action以常规方式获取JSON数据">测试从Action以常规方式获取JSON数据</a>

</body>

</html>

5、pom.xml中添加tomcat-maven-plugin的plugin

            <plugin>

                <groupId>org.codehaus.mojo</groupId>

                <artifactId>tomcat-maven-plugin</artifactId>

                <version>1.0-beta-1</version>

                <configuration>

                    <url>http://localhost:8080/manager/text</url>

                    <path>/ ReportWebApp</path>

                    <server>tomcat</server>

                    <username>admin</username>

                    <password>password</password>

                </configuration>

            </plugin>

6、Right Click >> Run As >> Maven Build,在窗口中的Goals内输入:package tomcat:redeploy

三、采用Log4j2实现日志功能

1、指定目录下创建log4j2.xml,内容如下:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appenders>

        <RollingFile name="outErrorFile" fileName="RWA_logs/exception.log"

            filePattern="RWA_logs/$${date:yyyy-MM}/exception-%d{MM-dd-yyyy}-%i.log.gz">

            <PatternLayout

                pattern="%d{yyyy.MM.dd HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n" />

            <SizeBasedTriggeringPolicy size="10MB" />

        </RollingFile>

 

        <RollingFile name="outDebugFile" fileName="RWA_logs/debug.log"

            filePattern="RWA_logs/$${date:yyyy-MM}/exception-%d{MM-dd-yyyy}-%i.log.gz">

            <PatternLayout

                pattern="%d{yyyy.MM.dd HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n" />

            <SizeBasedTriggeringPolicy size="10MB" />

        </RollingFile>

 

        <RollingFile name="outInfoFile" fileName="RWA_logs/info.log"

            filePattern="RWA_logs/$${date:yyyy-MM}/exception-%d{MM-dd-yyyy}-%i.log.gz">

            <PatternLayout

                pattern="%d{yyyy.MM.dd HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n" />

            <SizeBasedTriggeringPolicy size="10MB" />

        </RollingFile>

 

        <Console name="consolePrint" target="SYSTEM_OUT">

            <PatternLayout

                pattern="%d{yyyy.MM.dd HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n" />

        </Console>

    </appenders>

    <loggers>

        <root level="DEBUG">

            <appender-ref level="DEBUG" ref="outDebugFile" />

            <appender-ref level="ERROR" ref="outErrorFile" />

            <appender-ref level="INFO" ref="outInfoFile" />

        </root>

    </loggers>

</configuration>

2、需打印日志的Class中添加成员变量代码

private static Logger log = LogManager.getLogger(ReportInfo.class.getName());

3、打印日志方法如下

log.debug("通过action是以传统方式返回JSON数据");

4C:\tomcat\bin\RWA_logs目录下查看日志是否生成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值