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 Java class testaction, JavaCodeIs:
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;
Private long results;
Private testdao = new testdao ();
Private 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 ){
This. testbean = testbean;
}
Public list getlist (){
Return list;
}
Public void setlist (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>