日期:2014-05-16  浏览次数:20391 次

搭建EXTJS和STRUTS2框架(ext和struts2简单实例)
新建一个工程struts2工程teaweb(因为现在所做的项目是一个关于茶叶,茶文化的),导入jar包(基本的几个jar包:commons-logging-1.0.4.jar,freemarker- 2.3.8.jar,ognl-2.6.11.jar,struts2-core-2.0.10.jar,xwork-2.0.4.jar),配置 struts.xml配置内容如下

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

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.action.extension" value="ph" />
<constant name="struts.multipart.maxSize" value="1000000000"/>
<package name="teaweb" extends="json-default" namespace="/">
<action name="test" class="com.teaweb.action.TestAction">

<result type="json"></result>
</action>
</package>
</struts>

注意此处的:extends="json-default" ,<result type="json"></result>

配置web.xml,内容如下:

<?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">

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.ph</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/struts-tags.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tags.tld</taglib-location>
</taglib>
</jsp-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

新建一个java类为TestAction,java代码为:

package com.teaweb.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import com.teaweb.bean.TestBean;
import com.teaweb.dao.TestDAO;

public class TestAction extends PublicAction {
private TestBean testBean;
private long results;
private TestDAO testDAO=new TestDAO();
private List list;
public String select() {
// TODO Auto-generated method stub
response.setCharacterEncoding("gb2312");
list=testDAO.select();
results=list.size();
return SUCCESS;
}
public String login() {
// TODO Auto-generated method stub
try {
request.setCharacterEncoding("utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setCharacterEncoding("gb2312");
TestBean result=testDAO.selectbyname(testBean);
if(result!=null){
outString("{success:true,msg:'"+result.getName()+"登录成功'}");
}else{
outString("{failure:true,msg:'登录失败'}");
}
return null;
}


public TestBean getTestBean() {
return testBean;
}

public void setTestBean(TestBean testBean) {
this.testBean = testBean;
}

public List getList() {
return list;
}

public void setList(List list) {
this.list = list;
}

public long getResults() {
return results;
}

public void setResults(long results) {
this.results = results;
}


}

其中TestBean 是一个实体类,还有一个连接数据库查询的方法,只要能查出为List结果就可以了

我这里做了一个登陆和查询所有TEST表里的信息两个方法

其中login.jsp代码为:

<%@ page language="java" import="java