struts代码(strut)
admin 发布:2022-12-19 23:59 96
今天给各位分享struts代码的知识,其中也会对strut进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
jsp代码如何转化为struts代码
用struts标签是么???
jsp中用struts标签就好了啊
jsp开始写上这句
%@ taglib prefix="s" uri="/struts-tags"%
导入struts的jar包.
然后配置好了,就可以用了
struts分页代码
OKOK分页实例代码
1 Book.java
package bean;
import java.sql.*;
import java.util.ArrayList;
/**
* Struts分页显示数据Bean,对应数据库中Book表
*/
public class Book {
private String bookname; //书名
private String author; //作者
private String price; //价格
public Book(String name,String author,String price){
this.bookname=name;
this.author=author;
this.price=price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getPrice(){
return this.price;
}
public void setPrice(String price){
this.price=price;
}
public static ArrayList getAllBook(Connection connection){
String sql="select * from book";
ArrayList arrayList = new ArrayList();
try{
Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet resultSet = statement.executeQuery(sql);
System.out.println("BookBean 数据查询已完成!");
while(resultSet.next())
{
String name = resultSet.getString("name");
String author = resultSet.getString("author");
String price = resultSet.getString("price");
System.out.println("开始数据封装:name="+name+"author="+author+"price="+price);
Book book = new Book(name,author,price);
arrayList.add(book);
}
connection.close();
resultSet.close();
}catch(SQLException e)
{
System.out.println("数据库异常"+e.toString());
}
return arrayList;
}
}
2 PageBean.java
package page;
import bean.Book;
import java.util.*;
/**
* Struts分页显示逻辑Bean
*/
public class PageBean {
int currentPage=1; //当前页
public int totalPages=0; //总页数
int pageRecorders=5;//每页5条数据
int totalRows=0; //总数据数
int pageStartRow=0;//每页的起始数
int pageEndRow=0; //每页显示数据的终止数
boolean hasNextPage=false; //是否有下一页
boolean hasPreviousPage=false; //是否有前一页
ArrayList arrayList;
Iterator it;
public PageBean(){}
public PageBean(ArrayList arrayList){
this.arrayList=arrayList;
totalRows=arrayList.size();
it=arrayList.iterator();
hasPreviousPage=false;
currentPage=1;
if((totalRows%pageRecorders)==0)
{
totalPages=totalRows/pageRecorders;
}
else
{
totalPages=totalRows/pageRecorders+1;
}
if(currentPage=totalPages)
{
hasNextPage=false;
}
else
{
hasNextPage=true;
}
if(totalRowspageRecorders)
{
this.pageStartRow=0;
this.pageEndRow=totalRows;
}
else
{
this.pageStartRow=0;
this.pageEndRow=pageRecorders;
}
}
/**
* @return Returns the currentPage.
*/
public String getCurrentPage() {
return this.toString(currentPage);
}
/**
* @param currentPage The currentPage to set.
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
/**
* @return Returns the pageRecorders.
*/
public int getPageRecorders() {
return pageRecorders;
}
/**
* @param pageRecorders The pageRecorders to set.
*/
public void setPageRecorders(int pageRecorders) {
this.pageRecorders = pageRecorders;
}
/**
* @return Returns the pageEndRow.
*/
public int getPageEndRow() {
return pageEndRow;
}
/**
* @return Returns the pageStartRow.
*/
public int getPageStartRow() {
return pageStartRow;
}
/**
* @return Returns the totalPages.
*/
public String getTotalPages() {
return this.toString(totalPages);
}
/**
* @return Returns the totalRows.
*/
public String getTotalRows() {
return this.toString(totalRows);
}
/**
* @return Returns the hasNextPage.
*/
public boolean isHasNextPage() {
return hasNextPage;
}
/**
* @param hasNextPage The hasNextPage to set.
*/
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
/**
* @return Returns the hasPreviousPage.
*/
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
/**
* @param hasPreviousPage The hasPreviousPage to set.
*/
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
public Book[] getNextPage(){
currentPage=currentPage+1;
System.out.println("PageBean.getNextPage()正在执行;");
System.out.println("参数currentPage="+currentPage);
if((currentPage-1)0)
{
hasPreviousPage=true;
}
else
{
hasPreviousPage=false;
}
if(currentPage=totalPages)
{
hasNextPage=false;
}
else
{
hasNextPage=true;
}
System.out.println("参数hasNextPage="+hasNextPage);
System.out.println("准备执行PageBean.getBooks()");
Book[] books=getBooks();
this.description();
return books;
}
public Book[] getPreviouspage(){
currentPage=currentPage-1;
if(currentPage==0){currentPage=1;}
if(currentPage=totalPages)
{
hasNextPage=false;
}
else
{
hasNextPage=true;
}
if((currentPage-1)0)
{
hasPreviousPage=true;
}
else
{
hasPreviousPage=false;
}
Book[] books=getBooks();
this.description();
return books;
}
public Book[] getBooks(){
System.out.println("pageBean.getBooks()开始执行;");
if(currentPage*pageRecorderstotalRows){//判断是否为最后一页
pageEndRow=currentPage*pageRecorders;
pageStartRow=pageEndRow-pageRecorders;
}
else{
pageEndRow=totalRows;
pageStartRow=pageRecorders*(totalPages-1);
}
Book[] books=new Book[pageEndRow-pageStartRow+1];
System.out.println("pageStartRow="+pageStartRow);
System.out.println("pageEndRow="+pageEndRow);
int j=0;
for(int i=pageStartRow;ipageEndRow;i++)
{
Book book=(Book)arrayList.get(i);
books[j++]=book;
}
System.out.println("要显示的页面数据已经封装,具体信息如下:");
this.description();
return books;
}
public String toString(int temp)
{
String str=Integer.toString(temp);
return str;
}
public void description()
{
String description="共有数据数:"+this.getTotalRows()+
"共有页数: "+this.getTotalPages() +
"当前页数为:"+this.getCurrentPage()+
" 是否有前一页: "+this.isHasPreviousPage() +
" 是否有下一页:"+this.isHasNextPage()+
" 开始行数:"+this.getPageStartRow()+
" 终止行数:"+this.getPageEndRow();
System.out.println(description);
}
}
3 PageListAction.java
package page;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import comm.Constants;
import bean.Book;
import java.util.*;
import javax.sql.DataSource;
/**
* Struts分页显示Action
*/
public class PageListAction extends Action {
public PageListAction(){}
ArrayList arrayList=new ArrayList();
PageBean pb;
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
String action;
action=request.getParameter("action");
if(action==null || action.equals("null")){ //第一次读取数据
try{
DataSource datasource=this.getDataSource(request,Constants.DATASOURCE_KEY);
arrayList=Book.getAllBook(datasource.getConnection());
System.out.println("第一步,数据已经成功传递到Action,action="+action);
}catch(Exception e){
e.printStackTrace();
System.out.println("数据库连接出现异常");
}
pb=new PageBean(arrayList);
Book[] books=pb.getBooks();
pb.description();
request.setAttribute("result",books);
request.setAttribute("page",pb);
}
else
{
if(action=="nextPage" || action.equals("nextPage"))
{
System.out.println("参数action="+action);
System.out.println("函数pb.getNextPage()准备执行");
Book[]books=pb.getNextPage();
request.setAttribute("page",pb);
request.setAttribute("result",books);
}
if(action=="previousPage" || action.equals("previousPage"))
{
System.out.println("参数action="+action);
System.out.println("函数pb.getPreviouspage()准备执行");
Book[] books=pb.getPreviouspage();
request.setAttribute("page",pb);
request.setAttribute("result",books);
}
}
return (mapping.findForward("success"));
}
}
4 pagetest.jsp
%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %
%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %
%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %
%@ page contentType="text/html; charset=gb2312" language="java"%
html:html locale="true"
head
meta http-equiv="Content-Type" content="text/html; charset=gb2312"
/head
body
table border="1"
trth书名/thth作者/thth价格/th/tr
logic:present name="result"
logic:iterate id="book" name="result" type="bean.Book"
logic:present name="book"
tr
tdbean:write name="book" property="bookname" //td
td bean:write name="book" property="author" //td
tdbean:write name="book" property="price" //td
/tr
/logic:present
/logic:iterate
/logic:present
/table
logic:equal name="page" property="hasNextPage" value="true"
html:link page="/page.do?action=nextPage"nextPage/html:link
/logic:equal
logic:equal name="page" property="hasPreviousPage" value="true"
html:link page="/page.do?action=previousPage"PreviousPage/html:link
/logic:equal
共有数据总数bean:write name="page" property="totalRows"/;
共分bean:write name="page" property="totalPages"/页,当前是第
bean:write name="page" property="currentPage"/页
/body
/html:html
5 struts-config.xml
?xml version="1.0" encoding="ISO-8859-1" ?
!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
""
struts-config
data-sources
data-source key="dataSource" type="org.apache.commons.dbcp.BasicDataSource"
set-property property="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/
set-property property="url" value="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=eBookStore;SelectMethod=cursor"/
set-property property="username" value="limq"/
set-property property="password" value="1"/
set-property property="maxActive" value="10"/
set-property property="maxWait" value="5000"/
set-property property="defaultAutoCommit" value="true"/
set-property property="defaultReadOnly" value="false"/
/data-source
/data-sources
form-beans
/form-beans
global-forwards
/global-forwards
action-mappings
action path="/page" type="page.PageListAction" scope="request"
forward name="success" path="/pagetest.jsp"/
/action
/action-mappings
controller
/controller
/struts-config
6 建立eBookStore数据库,以及表book(name,author,parce);其中数据的配置可以根据你的不同情况在struts-config.xml中而定。
7 Constants.java
package comm;
/**
* this interface provides the constant string for applicator constant
*/
public class Constants {
/**
* name of the User Object in HttpSession
*/
public static String USER_KEY="user";
/**
* dataSource name
*/
public static String DATASOURCE_KEY="dataSource";
}
java web中struts框架的具体代码是什么
struts的作用主要是搭建多层框架(一般是3层),struts没有什么代码,只是几个配置文件,按照3层框架一层引用一层的情况搭配的
例如:
bean name="loginAction" class="com.hanqian.action.LoginAction"
property name="loginBusiness" ref="loginBusiness" /
/bean
bean id="loginBusiness" class="com.hanqian.business.LoginBusiness"
property name="loginMgr" ref="loginMgr" /
/bean
bean id="loginMgr" class="com.hanqian.manager.LoginMgr"
property name="userDao" ref="DbUsersDAO" /
/bean
哪位大虾给解释一下下面的struts代码?
1.获得session ,java有2种方法。
request.getSession(true);
request.getServletContext().getSession();
2.转发。mapping.findForward("welcome");
这里需要看 WEB-INF/strutsConfig.xml 文件。
action path="/search"
type="addressbook.actions.SearchAction"
name="searchForm"
scope="request"
validate="true"
input="/search.jsp"
forward name="welcome" path="/display.jsp"/
!--看到没有,这里就是转发到哪里去--
/action
struts代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于strut、struts代码的信息别忘了在本站进行查找喔。
版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;
相关推荐
- 05-05小说源码里面的下载代码(小说 源代码)[20240505更新]
- 05-05网页图片位置代码(网页设计中图片的位置)[20240505更新]
- 05-05关于nat打洞代码的信息[20240505更新]
- 05-05实现导航界面代码(导航用户界面)[20240505更新]
- 05-05酷代码网站(很酷的代码)[20240505更新]
- 05-05数字货币最新开源代码(数字货币开源代码公布意味着什么)[20240505更新]
- 05-05jquery代码交互(jquery前后端交互)[20240505更新]
- 05-05dreamweaver压缩代码(dreamweaver格式化代码)[20240505更新]
- 05-05网页代码表(网页代码表格边框)[20240505更新]
- 05-05joomlaphp开发代码控制的简单介绍[20240505更新]
取消回复欢迎 你 发表评论:
- 标签列表
- 最近发表
- 友情链接