我用的是分派action、我在分派Acttion中写了两个(get和AddGoods)方法、(这两个方法有怎么用?)代码如下:
LoginAction:
package com.web.action;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;public class LoginAction extends DispatchAction {
public ActionForward get(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//保存令牌(保存在jsp动态生成的32位jsessionid)\
this.saveToken(request);
System.out.println("begin save");
return mapping.findForward("login");
}
//添加商品
public ActionForward AddGoods(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
GoodsInfo gf=(GoodsInfo) form;
GoodsDao ad=new GoodsDao();
HttpSession session =request.getSession();
String varietys=request.getParameter("variety");
String varietyss=request.getParameter("variety");
gf.setVariety(varietys.trim()+"-"+varietyss.trim());
gf.setGtime(regtime);
FormFile ff = gf.getFile();
System.out.println(ff.getContentType() + " " + ff.getFileName()
+ " " + ff.getFileSize());
try {
InputStream is = ff.getInputStream();
String path = this.getServlet().getServletContext().getRealPath("images");
File director = new File(path);
FileOutputStream fos = new FileOutputStream(new File(director, ff.getFileName()));
int b = -1;
while ((b = is.read()) != -1) {
fos.write(b);
}
is.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
gf.setPicture("images/" + ff.getFileName());
//1、boolean b=ad.AddGoods(gf);
//2、 if(b){
//3、 return mapping.findForward("succeed"); //如果添加商品成功就去首页显示、以前没用令牌处理、我添加成功后、在首页IE工具栏点击刷新、他就会再次向数据库插入同一条值、如果唯一就会报错、(是否在首页要调用分派action中的get方法???要的话又怎么调?)
} //4、 return mapping.findForward("baulk");//跳失败页面
}
怎样把1、2、3、4、这4行代码放到下面的令牌代码但中去进行判断????
可不可以把令牌的判断和我注册的代码一起判断例如if(this.isTokenValid(request,true)&&b==true)
//令牌代码
if(this.isTokenValid(request,true))
{
System.out.println("valid");
return mapping.findForward("succeed");
}
else
{
System.out.println("invalid");
return mapping.findForward("baulk");
}
}
}
-----------struts-config.xml配置文件---------
struts-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"><struts-config>
<data-sources />
<form-beans>
<form-bean name="goodsForm" type="com.dress.Entity.GoodsInfo" /> </form-beans>
<global-exceptions />
<global-forwards />
<action attribute="goodsForm" input="/goods/addgoods.jsp" name="goodsForm"
path="/goods" parameter="gs" scope="request"
type="com.dress.struts.action.ClientAction">
<set-property property="cancellable" value="true" />
<forward name="succeed" path="/index.jsp" />
<forward name="baulk" path="/b.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.dress.struts.ApplicationResources" />
</struts-config>
-----------------------注册JSP页面Addgoods.jsp
我在添加商品的页面是否也要调用分派Action中get方法、如果要又怎样写?
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<base href="<%=basePath%>">
<title>My JSP 'Addgoods.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<br/>
<logic:present name="error">
<pre style="color:#ff2255"><bean:write name="error"/></pre>
</logic:present>
<html:form method="post" action="goods.do?gs=AddGoods" enctype="multipart/form-data">
<table border="1" align="center" >
<tr>
<td>商品名:</td>
<td><html:text property="gname"/></td>
</tr>
<tr>
<td>尺寸大小:</td>
<td><html:select property="gsize">
<option>xx</option>
<option>xm</option>
<option>ml</option>
<option>cd</option>
</html:select></td>
</tr>
<tr>
<td>商品颜色: </td>
<td><html:text property="color" /></td>
</tr>
<tr>
<td>品牌: </td>
<td><html:text property="brand" /></td>
</tr>
<tr>
<td>价格: </td>
<td>
<html:text property="price"/>
</td>
</tr>
<tr>
<td>图片:</td>
<td><html:file property="file" /></td>
</tr>
<tr>
<td>种类:</td>
<td>
大类:<html:select property="variety">
<option>外套</option>
<option>上衣</option>
<option>连衣裙</option>
<option>裤装</option>
<option>首饰</option>
<option>男装</option>
<option>女装</option>
<option>配饰</option>
</html:select>
小类 :<html:select property="variety">
<option>夹克</option>
<option>休闲上衣</option>
<option>开襟衫</option>
<option>大衣</option>
<option>T恤</option>
<option>针织衫</option>
<option>坎肩马甲</option>
<option>衬衫</option>
<option>连衣裙A</option>
<option>连衣裙B</option>
<option>牛仔裙</option>
<option>裙装</option>
<option>长裤</option>
<option>短裤</option>
<option>七分裤</option>
<option>丝袜</option>
<option>耳环</option>
<option>项链</option>
<option>外套/上装</option>
<option>裤装/配饰</option>
<option>鞋/围巾</option>
<option>帽子/礼包</option>
<option>鞋子</option>
<option>包包</option>
<option>围巾</option>
<option>帽子</option>
</html:select>
</td>
</tr>
<tr>
<td>商品介绍:</td>
<td><html:textarea rows="3"property="detailedInfo"></html:textarea></td>
</tr>
<tr>
<td>商品产地: </td>
<td><html:text property="address"/></td>
</tr>
<tr>
<td height="20" colspan="2" align="center">
<input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value="d7484f95247cf242a6f35107a1c7ac25">
<html:reset property="reset" value="重置" />
<html:submit property="Submit" value="提交" />
<!-- <span> <a href="goods/Showbook.jsp?bame=${bame}" target="_blank">显示</a></span> -->
<html:button property="fanh" value="返回" onclick="history.back()"/>
</tr>
</table>
</html:form>
</body>
</html:html>
我是个初学者、请教各位高手!
LoginAction:
package com.web.action;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;public class LoginAction extends DispatchAction {
public ActionForward get(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//保存令牌(保存在jsp动态生成的32位jsessionid)\
this.saveToken(request);
System.out.println("begin save");
return mapping.findForward("login");
}
//添加商品
public ActionForward AddGoods(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
GoodsInfo gf=(GoodsInfo) form;
GoodsDao ad=new GoodsDao();
HttpSession session =request.getSession();
String varietys=request.getParameter("variety");
String varietyss=request.getParameter("variety");
gf.setVariety(varietys.trim()+"-"+varietyss.trim());
gf.setGtime(regtime);
FormFile ff = gf.getFile();
System.out.println(ff.getContentType() + " " + ff.getFileName()
+ " " + ff.getFileSize());
try {
InputStream is = ff.getInputStream();
String path = this.getServlet().getServletContext().getRealPath("images");
File director = new File(path);
FileOutputStream fos = new FileOutputStream(new File(director, ff.getFileName()));
int b = -1;
while ((b = is.read()) != -1) {
fos.write(b);
}
is.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
gf.setPicture("images/" + ff.getFileName());
//1、boolean b=ad.AddGoods(gf);
//2、 if(b){
//3、 return mapping.findForward("succeed"); //如果添加商品成功就去首页显示、以前没用令牌处理、我添加成功后、在首页IE工具栏点击刷新、他就会再次向数据库插入同一条值、如果唯一就会报错、(是否在首页要调用分派action中的get方法???要的话又怎么调?)
} //4、 return mapping.findForward("baulk");//跳失败页面
}
怎样把1、2、3、4、这4行代码放到下面的令牌代码但中去进行判断????
可不可以把令牌的判断和我注册的代码一起判断例如if(this.isTokenValid(request,true)&&b==true)
//令牌代码
if(this.isTokenValid(request,true))
{
System.out.println("valid");
return mapping.findForward("succeed");
}
else
{
System.out.println("invalid");
return mapping.findForward("baulk");
}
}
}
-----------struts-config.xml配置文件---------
struts-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"><struts-config>
<data-sources />
<form-beans>
<form-bean name="goodsForm" type="com.dress.Entity.GoodsInfo" /> </form-beans>
<global-exceptions />
<global-forwards />
<action attribute="goodsForm" input="/goods/addgoods.jsp" name="goodsForm"
path="/goods" parameter="gs" scope="request"
type="com.dress.struts.action.ClientAction">
<set-property property="cancellable" value="true" />
<forward name="succeed" path="/index.jsp" />
<forward name="baulk" path="/b.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.dress.struts.ApplicationResources" />
</struts-config>
-----------------------注册JSP页面Addgoods.jsp
我在添加商品的页面是否也要调用分派Action中get方法、如果要又怎样写?
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<base href="<%=basePath%>">
<title>My JSP 'Addgoods.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<br/>
<logic:present name="error">
<pre style="color:#ff2255"><bean:write name="error"/></pre>
</logic:present>
<html:form method="post" action="goods.do?gs=AddGoods" enctype="multipart/form-data">
<table border="1" align="center" >
<tr>
<td>商品名:</td>
<td><html:text property="gname"/></td>
</tr>
<tr>
<td>尺寸大小:</td>
<td><html:select property="gsize">
<option>xx</option>
<option>xm</option>
<option>ml</option>
<option>cd</option>
</html:select></td>
</tr>
<tr>
<td>商品颜色: </td>
<td><html:text property="color" /></td>
</tr>
<tr>
<td>品牌: </td>
<td><html:text property="brand" /></td>
</tr>
<tr>
<td>价格: </td>
<td>
<html:text property="price"/>
</td>
</tr>
<tr>
<td>图片:</td>
<td><html:file property="file" /></td>
</tr>
<tr>
<td>种类:</td>
<td>
大类:<html:select property="variety">
<option>外套</option>
<option>上衣</option>
<option>连衣裙</option>
<option>裤装</option>
<option>首饰</option>
<option>男装</option>
<option>女装</option>
<option>配饰</option>
</html:select>
小类 :<html:select property="variety">
<option>夹克</option>
<option>休闲上衣</option>
<option>开襟衫</option>
<option>大衣</option>
<option>T恤</option>
<option>针织衫</option>
<option>坎肩马甲</option>
<option>衬衫</option>
<option>连衣裙A</option>
<option>连衣裙B</option>
<option>牛仔裙</option>
<option>裙装</option>
<option>长裤</option>
<option>短裤</option>
<option>七分裤</option>
<option>丝袜</option>
<option>耳环</option>
<option>项链</option>
<option>外套/上装</option>
<option>裤装/配饰</option>
<option>鞋/围巾</option>
<option>帽子/礼包</option>
<option>鞋子</option>
<option>包包</option>
<option>围巾</option>
<option>帽子</option>
</html:select>
</td>
</tr>
<tr>
<td>商品介绍:</td>
<td><html:textarea rows="3"property="detailedInfo"></html:textarea></td>
</tr>
<tr>
<td>商品产地: </td>
<td><html:text property="address"/></td>
</tr>
<tr>
<td height="20" colspan="2" align="center">
<input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value="d7484f95247cf242a6f35107a1c7ac25">
<html:reset property="reset" value="重置" />
<html:submit property="Submit" value="提交" />
<!-- <span> <a href="goods/Showbook.jsp?bame=${bame}" target="_blank">显示</a></span> -->
<html:button property="fanh" value="返回" onclick="history.back()"/>
</tr>
</table>
</html:form>
</body>
</html:html>
我是个初学者、请教各位高手!
参考下
你主要在你不同的方法中得需要设置saveToken(request);