Create a new project struts2 project teaweb (because the project is now done on a tea, tea culture), import jar packages (Basic jar packages: 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), configure struts. the xml configuration is as follows:
<? 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>
Note the following:Extends = "json-default", <result type = "json"> </result>
Configure web. xml as follows:
<? 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>
Create a new java class TestAction, And the java code is:
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 () + "Logon successful '}");
} Else {
OutString ("{failure: true, msg: 'logon failed '}");
}
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 is an entity class, and there is a method to connect to the database for query, as long as the List result can be found.
Here I have made two methods to log on and query information in all the TEST tables.
The login. jsp code is:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> My JSP 'login. jsp 'starting page </title>
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "This is my page">
<Link rel = "stylesheet" type = "text/css" href = "/ext2/resources/css/ext-all.css"/>
<Script type = "text/javascript" src = "/ext2/adapter/ext/ext-base.js"> </script>
<Script type = "text/javascript" src = "/ext2/ext-all.js"> </script>
</Head>
<Body>
<Script type = "text/javascript">
Ext. onReady (function (){
Ext. QuickTips. init ();
Var form1 = new Ext. FormPanel ({
RenderTo: "panel1 ",
Width: 500,
Height: 300,
Frame: true,
Title: "ajax submission ",
Collapsible: true,
MinButtonWidth: 60,
LabelAlign: "right ",
DefaultType: "textfield ",
Url: "test! Login. ph ",
Items :[{
FieldLabel: "User Name ",
Id: "txtName ",
Name: 'testbean. name ',
AllowBlank: false,
BlankText: "The user name cannot be blank! "
},{
FieldLabel: "password ",
AllowBlank: false,
BlankText: "The password cannot be blank! ",
Name: 'testbean. password ',
InputType: 'Password'
},{
FieldLabel: "Remarks"
}],
Buttons :[{
Text: "Submit ",
Handler: function (){
If (form1.getForm (). isValid ()){
Form1.getForm (). submit ({
Success: function (from, action ){
Ext. Msg. alert ("Return prompt", action. result. msg );
Window. location = 'index. jsp ';
},
Failure: function (form, action ){
Ext. Msg. alert ("Return prompt", action. result. msg );
}
});
}
}
},{
Text: "reset ",
Handler: function (){
Form1.getForm (). reset ();
}
}]
});
});
</Script>
<Div id = "panel1"> </div>
</Body>
</Html>
The index. jsp page code is:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> index </title>
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "This is my page">
<Link rel = "stylesheet" type = "text/css" href = "/ext2/resources/css/ext-all.css"/>
<Script type = "text/javascript" src = "/ext2/adapter/ext/ext-base.js"> </script>
<Script type = "text/javascript" src = "/ext2/ext-all.js"> </script>
</Head>
<Body>
<Script type = "text/javascript">
Ext. onReady (function (){
Var store = new Ext. data. JsonStore ({
Url: "test! Select. ph ",
TotalProperty: "results ",
Root: "list ",
Fields: [{name: 'id', mapping: 'id'}, {name: 'name', mapping: 'name'}, {name: 'Password', mapping: 'Password'}]
});
Store. load ();
Var gird = new Ext. grid. GridPanel ({
RenderTo: "hello ",
Title: "Welcome to log on ",
Height: 150,
Width: 300,
Columns :[
{Header: "No.", dateindex: "id "},
{Header: "Account", dateindex: "name "},
{Header: "password", dateindex: "password "}
],
Store: store,
AutoExpandColumn: 2
})
})
</Script>
<Div id = "hello"> </div>
</Body>
</Html>