JAVA涂鸦
关于JAVA的点点滴滴
posts - 50,  comments - 689,  trackbacks - 0
接上一篇:struts+spring+hibernate的web应用<四> Web层代码编写(1)

接着就是写资源文件了。

com.game.resources包中添加ApplicationResourcesProducts.properties文件。如果你的eclipse中有Properties Editor这个插件,就可以用Properties Editor打开资源文件进行编写,省去了用命令行转换格式的麻烦。配置内容如下:

errors.required={0}不能为空.
errors.minlength={0}不能小于 {1} 个字符.
errors.maxlength={0}不能大于 {1} 个字符.
errors.invalid={0}输入格式不对.
errors.byte={0}必须为字节.
errors.short={0}必须为短整型.
errors.integer={0}必须为整型.
errors.long={0}必须为长整型.
errors.float={0}必须为浮点型.
errors.double={0}必须为双精度型.
errors.date={0}不是一个正确的日期或格式不对.
errors.range={0} is not in the range {1} through {2}.
errors.creditcard={0} is not a valid credit card number.
errors.email={0}不是一个有效的email地址.

errors.gameNameCn=游戏中文名称
errors.gameNameEn=游戏英文名称
errors.gameCapacity=碟数
errors.gamePrice=价格


 

需要注意的是

 ssh3.jpg

这一节是不能省去的。

 

接下来就是写页面文件了。在products文件夹中添加如下页面。

 

index.jsp页面代码:

<%@ page contentType="text/html;charset=GBK" language="java" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
<title>游戏软件管理系统</title>
</head>
<body>

<href="/game/getProducts.do?method=doGetProducts&&flag=1">进入游戏软件管理单元</a>
</body>
</html>

 

 

products.jsp页面代码:

<%@page pageEncoding="GBK" contentType="text/html; charset=GBK" import="com.game.commons.Pager;"%>
<%@ taglib uri="struts-html" prefix="html" %>
<%@ taglib uri="struts-logic" prefix="logic" %>
<%@ taglib uri="struts-bean" prefix="bean" %>
<%
String flag=(String)request.getAttribute("flag");
String totalRows=(String)request.getAttribute("totalRows");
String fieldname="";
String value="";
if(flag.equals("2")){
    fieldname
=(String)request.getAttribute("fieldname");
    value
=(String)request.getAttribute("value");
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
<title></title>
<script language = "JavaScript">
function btnQuery() {
    
if(myfm.value.value=="")
    
{    
        alert(
"请输入查询关键字!");
    }
else{
        window.location.href
="/game/queryProducts.do?method=doQueryProduct&&flag=2&&fieldname="+myfm.fieldname.value+"&&value="+myfm.value.value;
     }

 }

 
</script>
</head>
<body>
<form  name="myfm" method="post">
<table border="1">
<tr>
<td>
<href="/game/getProducts.do?method=doGetProducts&&flag=1">察看所有记录</a>
&nbsp;&nbsp;&nbsp;
</td>
<td>
<href="/game/addProductPage.do?method=doAddProductPage&&flag=<%= flag%>&&fieldname=<%=fieldname%>&&value=<%= value%>">添加</a>
&nbsp;&nbsp;&nbsp;
</td>
<td>
    
<select name="fieldname" >
        
<option value="gameNameCn">中文名称</option>
        
<option value="gameNameEn">英文名称</option>
        
<option value="gameCapacity">碟数</option>
        
<option value="gameVersion">版本</option>
        
<option value="gameMedia">介质</option>
        
<option value="gameCopyright">版权</option>
    
</select>
    
<input value="" name="value"  />

<href="#" onclick="btnQuery()">查询</a>
</td>
</tr>
</table>
<br>
<br>
  
<table border="1">
    
<tr>
        
<td>游戏中文名称</td>
        
<td>游戏英文名称</td>
        
<td>软件版本</td>
        
<td>软件介质</td>
        
<td>碟数</td>
        
<td>版权</td>
        
<td>价格(元)</td>
        
<td>攻略</td>
    
</tr>
    
<logic:iterate id="pd" name="productsList">
    
<tr>
        
<td><href="/game/getProduct.do?method=doGetProduct&&flag=<%=flag%>&&gameId=<bean:write name="pd" property="gameId"/>&&flag=<%= flag%>&&fieldname=<%=fieldname%>&&value=<%= value%>"><bean:write name="pd" property="gameNameCn"/></a></td>
        
<td><bean:write name="pd" property="gameNameEn"/></td>
        
<td>
            
<logic:equal name="pd" property="gameVersion" value="1">
                中文版
            
</logic:equal>
            
<logic:equal name="pd" property="gameVersion" value="2">
                英文版
            
</logic:equal>
        
</td>
        
<td>
            
<logic:equal name="pd" property="gameMedia" value="1">
                CD
            
</logic:equal>
            
<logic:equal name="pd" property="gameMedia" value="2">
                DVD5
            
</logic:equal>
            
<logic:equal name="pd" property="gameMedia" value="3">
                DVD9
            
</logic:equal>
        
</td>
        
<td><bean:write name="pd" property="gameCapacity"/></td>
        
<td>
            
<logic:equal name="pd" property="gameCopyright" value="1">
                正版
            
</logic:equal>
            
<logic:equal name="pd" property="gameCopyright" value="2">
                翻版
            
</logic:equal>
        
</td>
        
<td><bean:write name="pd" property="gamePrice"/></td>
        
<td><bean:write name="pd" property="gameContent"/></td>
    
</tr>
    
</logic:iterate>
    
<tr>
        
<%if(flag.equals("1")){%>
            
<td colspan="8" align="right" class="head">
                共有
<%=totalRows%>条记录&nbsp;&nbsp;&nbsp;&nbsp;
                第
<bean:write name="PAGER" property="currentPage"/>&nbsp;
                共
<bean:write name="PAGER" property="totalPages"/>&nbsp;
                
<html:link action="/getProducts.do?method=doGetProducts&&flag=1&&pagerMethod=first" paramName="PAGER" paramProperty="currentPage" paramId="currentPage">首页</html:link>
                
<html:link action="/getProducts.do?method=doGetProducts&&flag=1&&pagerMethod=previous" paramName="PAGER" paramProperty="currentPage" paramId="currentPage">上一页</html:link>
                
<html:link action="/getProducts.do?method=doGetProducts&&flag=1&&pagerMethod=next" paramName="PAGER" paramProperty="currentPage" paramId="currentPage">下一页</html:link>
                
<html:link action="/getProducts.do?method=doGetProducts&&flag=1&&pagerMethod=last" paramName="PAGER" paramProperty="currentPage" paramId="currentPage">尾页</html:link>
            
</td>    
        
<%}else if(flag.equals("2")){%>
        
<% Pager pager=(Pager)request.getAttribute("PAGER");%>
            
<td colspan="8" align="right" class="head">
                共有
<%=totalRows%>条记录&nbsp;&nbsp;&nbsp;&nbsp;
                第
<bean:write name="PAGER" property="currentPage"/>&nbsp;
                共
<bean:write name="PAGER" property="totalPages"/>&nbsp;
                
<href="/game/queryProducts.do?method=doQueryProduct&&flag=2&&fieldname=<%=fieldname%>&&value=<%= value%>&&pagerMethod=first&&currentPage=<%=pager.getCurrentPage()%>" >首页</a>
                
<href="/game/queryProducts.do?method=doQueryProduct&&flag=2&&fieldname=<%=fieldname%>&&value=<%= value%>&&pagerMethod=previous&&currentPage=<%=pager.getCurrentPage()%>" >上一页</a>
                
<href="/game/queryProducts.do?method=doQueryProduct&&flag=2&&fieldname=<%=fieldname%>&&value=<%= value%>&&pagerMethod=next&&currentPage=<%=pager.getCurrentPage()%>" >下一页</a>
                
<href="/game/queryProducts.do?method=doQueryProduct&&flag=2&&fieldname=<%=fieldname%>&&value=<%= value%>&&pagerMethod=last&&currentPage=<%=pager.getCurrentPage()%>" >尾页</a>
                
            
</td>    
        
<%}%>
    
</tr>
</table>
</form>
</body>
</html>



addproduct.jsp页面代码:

<%@page pageEncoding="GBK" contentType="text/html; charset=GBK" %>
<%@ taglib uri="struts-html" prefix="html" %>
<%@ taglib uri="struts-logic" prefix="logic" %>
<%@ taglib uri="struts-bean" prefix="bean" %>
<%
String flag=(String)request.getAttribute("flag");
String fieldname="";
String value="";
if(flag.equals("2")){
    fieldname
=(String)request.getAttribute("fieldname");
    value
=(String)request.getAttribute("value");
}
String maxid=(String)request.getAttribute("maxid");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
<title></title>
<html:base/>
<html:javascript formName="productsForm" method="validate"/>
<script Language="JavaScript">
       
function btn_AddFolder() {
           
if(confirm("确定要保存吗?")){
            
if(document.productsForm.onsubmit()){        
                document.forms[
0].submit();
            }

        }
  
    }

</script>
</head>
<body>

<href="/game/returnProduct.do?method=doReturnProduct&&flag=<%= flag%>&&fieldname=<%=fieldname%>&&value=<%= value%>">返回</a>
<html:form action="/addProduct.do" onsubmit="return validate(this)">
<table border="1">
    
<tr>
        
<td>游戏中文名称</td>
        
<td>游戏英文名称</td>
        
<td>软件版本</td>
        
<td>软件介质</td>
        
<td>碟数</td>
        
<td>版权</td>
        
<td>价格(元)</td>
        
<td>攻略</td>
    
</tr>
    
<tr>
        
<td><html:text property="gameNameCn"/></td>
        
<td><html:text property="gameNameEn"/></td>
        
<td>
            
<html:select property="gameVersion" >
                  
<html:option value= "2">英文版&nbsp;&nbsp;&nbsp;&nbsp;</html:option>
                  
<html:option value= "1">中文版</html:option>
              
</html:select>   
        
</td>
        
<td>
            
<html:select property="gameMedia" >
                  
<html:option value= "1">CD&nbsp;&nbsp;&nbsp;&nbsp;</html:option>
                  
<html:option value= "2">DVD5</html:option>
                  
<html:option value= "3">DVD9</html:option>
              
</html:select>   
        
</td>
        
<td><html:text property="gameCapacity" size="5"/></td>
        
<td>
            
<html:select property="gameCopyright" >
                  
<html:option value= "2">翻版&nbsp;&nbsp;&nbsp;&nbsp;</html:option>
                  
<html:option value= "1">正版</html:option>
              
</html:select>  
        
</td>
        
<td><html:text property="gamePrice"/></td>
        
<td><html:text property="gameContent"/></td>
    
</tr>
    
</table>
<html:hidden property="gameId" value="<%= maxid%>"/>
<html:hidden property="method" value="doAddProduct"/>
<html:hidden property="flag" value="<%= flag%>"/>
<html:hidden property="fieldname" value="<%= fieldname%>"/>
<html:hidden property="value" value="<%=value%>"/>
<input type="button" value="保存" onclick="btn_AddFolder()"/>

</html:form>
</body>
</html>


product.jsp页面代码:

<%@page pageEncoding="GBK" contentType="text/html; charset=GBK" %>
<%@ taglib uri="struts-html" prefix="html" %>
<%@ taglib uri="struts-logic" prefix="logic" %>
<%@ taglib uri="struts-bean" prefix="bean" %>
<%
String flag=(String)request.getAttribute("flag");
String fieldname="";
String value="";
if(flag.equals("2")){
    fieldname
=(String)request.getAttribute("fieldname");
    value
=(String)request.getAttribute("value");
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
<title></title>
<html:javascript formName="productsForm" method="validate"/>
</head>
<body>
<href="/game/addProductPage.do?method=doAddProductPage&&flag=<%= flag%>&&fieldname=<%=fieldname%>&&value=<%= value%>">添加</a> &nbsp;&nbsp;&nbsp;
<href="/game/returnProduct.do?method=doReturnProduct&&flag=<%= flag%>&&fieldname=<%=fieldname%>&&value=<%= value%>">返回</a>&nbsp;&nbsp;&nbsp;
<href="/game/deleteProduct.do?method=doDeleteProduct&&flag=<%= flag%>&&gameId=<bean:write name="pd" property="gameId"/>&&fieldname=<%=fieldname%>&&value=<%= value%>">删除</a>
<br>
<br>
<html:form action="/updateProduct.do" onsubmit="return validate(this)">
<table border="1">
    
<tr>
        
<td>游戏中文名称</td>
        
<td>游戏英文名称</td>
        
<td>软件版本</td>
        
<td>软件介质</td>
        
<td>碟数</td>
        
<td>版权</td>
        
<td>价格(元)</td>
        
<td>攻略</td>
    
</tr>
    
<tr>
        
<td><html:text name="pd" property="gameNameCn"/></td>
        
<td><html:text name="pd" property="gameNameEn"/></td>
        
<td>
            
<html:select name="pd" property="gameVersion" >
                  
<html:option value= "1">中文版&nbsp;&nbsp;&nbsp;&nbsp;</html:option>
                  
<html:option value= "2">英文版</html:option>
              
</html:select> 
        
</td>
        
<td>
            
<html:select name="pd" property="gameMedia" >
                  
<html:option value= "1">CD&nbsp;&nbsp;&nbsp;&nbsp;</html:option>
                  
<html:option value= "2">DVD5</html:option>
                  
<html:option value= "3">DVD9</html:option>
              
</html:select>  
        
</td>
        
<td><html:text name="pd" property="gameCapacity" size="5"/></td>
        
<td>
            
<html:select name="pd" property="gameCopyright" >
                  
<html:option value= "1">正版&nbsp;&nbsp;&nbsp;&nbsp;</html:option>
                  
<html:option value= "2">翻版</html:option>
              
</html:select>  
        
</td>
        
<td><html:text name="pd" property="gamePrice" size="10"/></td>
        
<td><html:text name="pd" property="gameContent"/></td>
    
</tr>
    
</table>
<html:hidden property="method" value="doUpdateProduct"/>
<html:hidden name="pd" property="gameId" />
<html:hidden property="flag" value="<%= flag%>"/>
<html:hidden property="fieldname" value="<%= fieldname%>"/>
<html:hidden property="value" value="<%=value%>"/>
<html:submit property="submit" value="保存"></html:submit>
</html:form>
</body>
</html>


product_success.jsp
页面代码:

<%@page pageEncoding="GBK" contentType="text/html; charset=GBK" %>
<%
String flag=(String)request.getAttribute("flag");
String fieldname="";
String value="";
if(flag.equals("2")){
    fieldname
=(String)request.getAttribute("fieldname");
    value
=(String)request.getAttribute("value");
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
<title></title>
</head>
<body>
操作成功!

<href="/game/returnProduct.do?method=doReturnProduct&&flag=<%= flag%>&&fieldname=<%=fieldname%>&&value=<%= value%>">返回</a>
<href="/game/addProductPage.do?method=doAddProductPage&&flag=<%= flag%>&&fieldname=<%=fieldname%>&&value=<%= value%>">继续添加</a>
</body>
</html>

 

product_failure.jsp页面代码:

 

<%@page pageEncoding="GBK" contentType="text/html; charset=GBK" %>
<%
String flag=(String)request.getAttribute("flag");
String fieldname="";
String value="";
if(flag.equals("2")){
    fieldname
=(String)request.getAttribute("fieldname");
    value
=(String)request.getAttribute("value");
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
<title></title>
</head>
<body>
操作失败!
<href="/game/returnProduct.do?method=doReturnProduct&&flag=<%= flag%>&&fieldname=<%=fieldname%>&&value=<%= value%>">返回</a>
</body>
</html>

 

最后在tld文件夹中添加下列tld文件。

 

 

至此,整个项目基本编写完了,运行tomcat,看看我们的项目的效果吧!

在地址栏输入http://localhost:8088/game/products/index.jsp

 

然后点击“进入游戏软件管理单元”链接,主界面出现了。


posted on 2007-03-14 10:25 千山鸟飞绝 阅读(7469) 评论(18)  编辑  收藏 所属分类: Web开发

FeedBack:
# re: struts+spring+hibernate的web应用 Web层代码编写(2)[未登录]
2007-03-14 13:24 | 阿蜜果
:)
可以试着将分页部分写成自定义标签的形式。  回复  更多评论
  
# re: struts+spring+hibernate的web应用 Web层代码编写(2)
2007-03-24 09:07 | 千山鸟飞绝
@阿蜜果
等有时间再来修改。  回复  更多评论
  
# re: struts+spring+hibernate的web应用 Web层代码编写(2)[未登录]
2007-04-22 15:41 | leo
页面最好不要有代码,后台处理下放到form里直接在页面显示不是很好吗.  回复  更多评论
  
# re: struts+spring+hibernate的web应用 Web层代码编写(2)
2007-04-26 14:18 | 军军
收益非浅
可以加10000个顶。
向你学习。  回复  更多评论
  
# re: struts+spring+hibernate的web应用 Web层代码编写(2)
2007-04-30 14:06 | furong
千山鸟飞绝
非常感谢你写的这些系列文章

但是感觉最后的web应用层代码很多,对于我这种初学者来说看起来有点困难
不知道你能不能把web层用struts2写出来,总感觉struts2可能没有这么烦琐

感激不尽!  回复  更多评论
  
# re: struts+spring+hibernate的web应用 Web层代码编写(2)
2007-04-30 16:09 | 千山鸟飞绝
@furong
你可以看看这个示例中的struts2.0的代码:
Struts2.0+spring2.0+hibernate3.1 ACEGI应用(权限判断)
http://www.blogjava.net/rickhunter/archive/2007/04/24/113103.html

最近比较忙,一直没时间写文章,只是将一些示例放在网上了。有空的时候会写几篇关于struts2.0和hibernate annotation以及spring2.0的文章。  回复  更多评论
  
# re: struts+spring+hibernate的web应用 Web层代码编写(2)
2007-05-09 09:50 | 东东
大体意思很清晰 比较有价值 爱  回复  更多评论
  
# re: struts+spring+hibernate的web应用 Web层代码编写(2)
2007-06-06 09:49 | neusoft029
非常漂亮,写的很好  回复  更多评论
  
# re: struts+spring+hibernate的web应用 Web层代码编写(2)
2007-06-14 11:53 | sgwood
你写得不错,支持一下,最近研究Struts2.0
有兴趣的人一起联系:sgwood@qq.com  回复  更多评论
  
# re: struts+spring+hibernate的web应用 Web层代码编写(2)
2007-06-27 15:48 | 卢涛
是的Web层的代码多了点.
建议JSP页面中使用JSTL,页面更加简洁  回复  更多评论
  
# re: struts+spring+hibernate的web应用 Web层代码编写(2)
2007-08-28 11:16 | kewon
org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 22 in the generated java file
The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory

Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:423)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:308)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
com.game.commons.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:36)

这是怎么原因?  回复  更多评论
  
# re: struts+spring+hibernate的web应用 Web层代码编写(2)
2007-08-28 11:17 | kewon
为什么jsp的页面不能编译?  回复  更多评论
  
# re: struts+spring+hibernate的web应用 Web层代码编写(2)[未登录]
2007-11-09 13:25 | sky
非常感谢  回复  更多评论
  
# re: struts+spring+hibernate的web应用 Web层代码编写(2)
2007-11-29 22:19 | 易京勇
基本思路还是很清晰的,学到东西拉!  回复  更多评论
  
# re: struts+spring+hibernate的web应用 Web层代码编写(2)
2008-04-25 07:44 | 问问
楼主,你的代码真的不怎么样(真的没啥先进的地方),也许用不了多久你就会赞同我的看法。
实话实说,请见谅。  回复  更多评论
  
# re: struts+spring+hibernate的web应用 Web层代码编写(2)[未登录]
2008-04-29 11:18 | aa
写得不错 顶起来   回复  更多评论
  
# re: struts+spring+hibernate的web应用 Web层代码编写(2)
2008-12-11 09:50 | Yu
虽然可能写的不完美,但是对于初学者来说,还是一个很好的入门例子,毕竟网上写得那些详细的教程很少,谢谢了:)  回复  更多评论
  
# re: struts+spring+hibernate的web应用 Web层代码编写(2)
2011-09-02 11:20 | 右手悬空
标记下,回去研究  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
正在阅读:



<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(35)

随笔档案

文章分类

文章档案

好友的blog

我的其他blog

老婆的Blog

搜索

  •  

积分与排名

  • 积分 - 771377
  • 排名 - 55

最新评论

阅读排行榜

评论排行榜