struts2.2 项目搭建 以及与 struts2-json jquery 前后台进行交互

1.新建web项目 引人10个jar包(在附件中下载),名称如下
commons-fileupload-1.2.2.jar 
commons-io-2.0.1.jar
commons-logging-1.1.1.jar
commons-lang3-3.1.jar
commons-lang-2.5.jar
freemarker-2.3.16.jar
ognl-3.0.1.jar
struts2-core-2.2.3.jar
xwork-core-2.2.3.jar
javassist-3.12.0.GA.jar

2.在src下 新建一个struts.xml 内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>
	<constant name="struts.action.extension" value="html" />
	<package name="sysdefault" extends="struts-default" abstract="true">
		<result-types>
			<result-type name="json" class="org.apache.struts2.json.JSONResult"/>
		</result-types>
		<interceptors>
			<interceptor-stack name="mystack">
				<interceptor-ref name="defaultStack" >
					<param name="exception.logEnabled">true</param>
            		<param name="exception.logLevel">error</param>					
				</interceptor-ref>
			</interceptor-stack>
			<interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/>
		</interceptors>
		<default-interceptor-ref name="mystack" />
	</package>
	
	<package name="zzghj_msg_test" extends="sysdefault">
		<action name="company" class="com.trt.CompanyAction" > 
			<result name="json" type="json"><param name="includeProperties">msg</param></result>
			<result name="index" >index.jsp</result>
		</action>
	</package>
</struts>


3.在web.xml配置  StrutsPrepareAndExecuteFilter,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.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>/*</url-pattern>
	</filter-mapping>
</web-app>


4.CompanyAction.java
/**
 * 
 */
package com.trt;

import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;

/**
 * @author Administrator
 *
 */
public class CompanyAction  extends AbstractAction{

	public String toIndex(){
		System.out.println("to index.jsp");
		return "index";
	}
	
	private String msg;
	public String testmsg() {
		String dport = paraPool.get("dport");
		String dpaud = paraPool.get("dpaud");
		String dphone = paraPool.get("dphone");
		System.out.println("dport:"+dport);
		System.out.println("dpaud:"+dpaud);
		System.out.println("dphone:"+dphone);
		JNative n = null;
		try {
			n = new JNative("mysms.dll", "InitModem");
			n.setRetVal(Type.INT);
			int i = 0;
			n.setParameter(i++, dport);
			n.setParameter(i++, dpaud);
			n.invoke();
			System.out.println(n.getRetValAsInt());
			String res = n.getRetVal();
			System.out.println("启动服务-返回结果:"+res);
			
			
			n = new JNative("mysms.dll", "SendSms");
			n.setRetVal(Type.INT);
			int j = 0;
			n.setParameter(j++, dport);
			n.setParameter(j++, dpaud);
			n.setParameter(j++, "Hello Wold");
			n.setParameter(j++, dphone);
			n.setParameter(j++, "FALSE");
			n.setParameter(j++, "FALSE");
			n.setParameter(j++, "FALSE");
			n.invoke();
			
			String res2 = n.getRetVal();
			System.out.println("发送结果:"+res2);
			msg = "启动结果:" + res + ",发送结果:" + res2;
		} catch (Exception e1) {
			System.out.println("启动服务出错了!");
			e1.printStackTrace();
			if (n != null) {
				try {
					n.dispose();// 释放资源
				} catch (NativeException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
			msg = "error";
			return JSON;
		}
		return JSON;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	
}



5.配置完后 直接访问:
http://localhost:8080/zzmsg/company!toIndex.html

如果能正常访问 ,恭喜你 struts2.2你已经配置成功了


如果想用jquery 的 json 前后台进行交互 ,还需要引人一个包
struts2-json-plugin-2.3.15.1.jar

如此你的jsp就可以这样写:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	String baseurl = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
%>
<c:set var="basepath" value="<%=request.getContextPath()%>" />
<c:set var="baseurl" value="<%=baseurl%>" />
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>网络报建系统</title>
 
<script src="${basepath}/common/js/base/jQuery.v1.4.2.js"></script>
<script src="${basepath}/common/js/base/jquery.form.js"></script>
<script src="${basepath}/common/js/base/common.js"></script>

<script>
	var telReg = /^1[3|4|5|8][0-9]\d{4,8}$/;
	var phoneReg = /^((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)$/;
	function add(){
		var url="${basepath}/company!testmsg.html";
	    var options = {
	        url: url,
	        type: 'post',
	        dataType: 'json',
	        success: function(data){
	        	if(data.msg=="error"){
					alert(data.msg);
				}
	            else{
	                alert("连接成功");
	                alert(data.msg);
	            }
	        }
	    };
	    $("form").ajaxSubmit(options);
	}
</script>
</head>
<body>
	<form method="post">
	<div class="">
  <div class="clear" style="height:50px;"></div>
  <div class="zghu_use_top"></div>
  <div class="zghu_use_mid">
    <div class="clear" style="height:15px;"></div>
    <div class="zghu_use_tit"><img src="${basepath}/common/images/company/13zghu_user_tit.png" /></div>
    <div class="clear" style="height:20px;"></div>
    <div class="zghu_use_stit">
      <div class="zghu_use_stcol01"><img src="${basepath}/common/images/company/13zghu_user_tu1.png" align="absmiddle" /></div>
       <div class="zghu_use_stcol02">短信测试</div>
    </div>
    <div class="clear" style="height:30px;"></div>
    <div class="zghu_use_row01">
      <div class="zghu_use_rtext">端口号:
      <input name="dport" id="dport" type="text" value="1" class="zghu_user_inp01" /></div>
      
    </div>
    <div class="clear" style="height:15px;"></div>
    <div class="zghu_use_row01">
      <div class="zghu_use_rtext">波特率:<input name="dpaud" id="dpaud" type="text" value="115200"  class="zghu_user_inp01"  /></div>
    </div>
    <div class="zghu_use_row01">
      <div class="zghu_use_rtext">手机:<input name="dphone" id="dphone" type="text" value="8615980842829"  class="zghu_user_inp01"  /></div>
    </div>
   <div class="clear" style="height:15px;"></div>
    <div class="zghu_use_row03" >
      <div class="zghu_use_thil"></div>
      <div class="zghu_use_thir" >&nbsp;&nbsp;
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="button" onclick="return add();" style="cursor: pointer;"  value="测试短信" /></div>
    </div>
    <div class="clear" style="height:30px;"></div>
  </div>
  <div class="zghu_use_btm"></div>
</div>
  	</form>
</body>


所需js  和 jar 包  在附件的测试例子demo中可以下载到。

你可能感兴趣的:(struts2.2)