truts-config.xml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"><struts-config>
<form-beans >
<form-bean name="itermForm" type="com.zjl.jc.web.form.ItemActionForm" />
<form-bean name="loginForm" type="com.zjl.jc.web.form.LoginForm" /> </form-beans> <global-exceptions />
<global-forwards>
<forward name="index" path="/index.jsp" redirect="true"/>
</global-forwards>
<action-mappings >
<action
parameter="command"
path="/iterm"
type="com.zjl.jc.web.action.ItermAction"
cancellable="true">
<forward name="list_success" path="/maint.jsp" />
<forward name="detail" path="/detail.jsp" />
<forward name="modify" path="/modify.jsp" />
<forward name="add" path="/add.jsp" />
<forward name="upload" path="/upload.jsp" />
</action>
<action path="/additem"
forward="/item_add.jsp"
name="itermForm"
>
</action>
<action
attribute="loginForm"
name="loginForm"
path="/login"
scope="request"
type="com.zjl.jc.web.action.LoginAction"
cancellable="true">
<forward name="error" path="/login_error.jsp" />
<forward name="success" path="/iterm.do?command=list" />
</action>
</action-mappings> <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/> <message-resources parameter="com.zjl.jc.web.ApplicationResources" />
</struts-config>ActionFormpackage com.zjl.jc.web.form;import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;public class ItemActionForm extends ActionForm {
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}public String getSpec() {
return spec;
}public void setSpec(String spec) {
this.spec = spec;
}public String getCategory() {
return category;
}public void setCategory(String category) {
this.category = category;
}public String getPattern() {
return pattern;
}public void setPattern(String pattern) {
this.pattern = pattern;
}public String getItermName() {
return itermName;
}public void setItermName(String itermName) {
this.itermName = itermName;
}
}DispacherActionpackage com.zjl.jc.web.action;import java.util.List;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;
import org.apache.struts.validator.DynaValidatorActionForm;import com.zjl.jc.model.Iterms;
import com.zjl.jc.service.ItermsManager;public class ItermAction extends DispatchAction {private ItermsManager manager;public void setManager(ItermsManager manager) {
this.manager = manager;
}public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
System.out.println("进来了没?");
// 两种情况
DynaValidatorActionForm f = (DynaValidatorActionForm)form;
String id = request.getParameter("id");Iterms i = null;
if (id == null) { // 新增的一个物料
i = new Iterms();
}else { // 修改的已有的物料 ,根据id读出来这条记录
i = manager.getIterms(id);
}i.setCategory((String)f.get("category"));
i.setItemName((String)f.get("itermName"));
i.setPattern((String)f.get("pattern"));
i.setSpec((String)f.get("spec"));manager.save(i);
return list(mapping, form, request, response);
}public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
//String id = request.getParameter("id");
manager.delete(request.getParameter("id"));
return list(mapping, form, request, response);
}public ActionForward edit(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// 得到要修改记录的id值
String id = request.getParameter("id");
DynaValidatorActionForm f = (DynaValidatorActionForm)form;
// 得到要修改的这条记录
Iterms i = manager.getIterms(id);
// 对这条记录进行属性的修改,完成后存放到f对象中,各个属性的值来自于这条记录的属性
f.set("itermName", i.getItemName());
f.set("spec", i.getSpec());
f.set("pattern", i.getPattern());
f.set("category", i.getCategory());request.setAttribute("id", id);return mapping.findForward("modify");
}
public ActionForward list(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
List list = manager.getIterms();
request.setAttribute("list", list);
return mapping.findForward("list_success");
}public ActionForward unspecified(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
return list(mapping, form, request, response);
}
}jsp页面 <%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>增加物料页面</title><script type="text/javascript">
function addItem() {if (document.getElementById("itemName").value == "") {
alert("物料名称不能为空!");
document.getElementById("itemName").focus();
return;
}
with (document.getElementById("itermForm")) {
method = "post";
action = "item.do?command=add";
submit();
}
}
function goBack() {
window.self.location = "item.do?command=list";
}</script>
</head>
<body>
<form name="itermForm" target="_self" id="itermForm"><table>
<tr>
<td height="26">物料名称:</td>
<td><input name="itemName" type="text" id="itemName" size="20" maxlength="20"></td>
</tr>
<tr>
<td height="26">物料规格:</td>
<td><label>
<input name="spec" type="text" id="spec" size="20" maxlength="20">
</label></td>
</tr>
<tr>
<td height="26">型 号:</td>
<td><input name="pattern" type="text" id="pattern" size="20" maxlength="20"></td>
</tr>
</table><hr width="97%" align="center" size=0> <input name="btnAdd" type="button" value="添加" id="btnAdd" onClick="addItem();" >
<input name="btnBack" type="button" value="返回" id="btnBack" onClick="goBack();">
</form>
</body>
</html>
web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 通过应用上下文参数来指定spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param><filter>
<filter-name>encoding</filter-name>
<filter-class>com.zjl.jc.service.util.FilterEncoding</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping><!-- 配置一个加载Spring应用上下文的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"><struts-config>
<form-beans >
<form-bean name="itermForm" type="com.zjl.jc.web.form.ItemActionForm" />
<form-bean name="loginForm" type="com.zjl.jc.web.form.LoginForm" /> </form-beans> <global-exceptions />
<global-forwards>
<forward name="index" path="/index.jsp" redirect="true"/>
</global-forwards>
<action-mappings >
<action
parameter="command"
path="/iterm"
type="com.zjl.jc.web.action.ItermAction"
cancellable="true">
<forward name="list_success" path="/maint.jsp" />
<forward name="detail" path="/detail.jsp" />
<forward name="modify" path="/modify.jsp" />
<forward name="add" path="/add.jsp" />
<forward name="upload" path="/upload.jsp" />
</action>
<action path="/additem"
forward="/item_add.jsp"
name="itermForm"
>
</action>
<action
attribute="loginForm"
name="loginForm"
path="/login"
scope="request"
type="com.zjl.jc.web.action.LoginAction"
cancellable="true">
<forward name="error" path="/login_error.jsp" />
<forward name="success" path="/iterm.do?command=list" />
</action>
</action-mappings> <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/> <message-resources parameter="com.zjl.jc.web.ApplicationResources" />
</struts-config>ActionFormpackage com.zjl.jc.web.form;import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;public class ItemActionForm extends ActionForm {
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}public String getSpec() {
return spec;
}public void setSpec(String spec) {
this.spec = spec;
}public String getCategory() {
return category;
}public void setCategory(String category) {
this.category = category;
}public String getPattern() {
return pattern;
}public void setPattern(String pattern) {
this.pattern = pattern;
}public String getItermName() {
return itermName;
}public void setItermName(String itermName) {
this.itermName = itermName;
}
}DispacherActionpackage com.zjl.jc.web.action;import java.util.List;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;
import org.apache.struts.validator.DynaValidatorActionForm;import com.zjl.jc.model.Iterms;
import com.zjl.jc.service.ItermsManager;public class ItermAction extends DispatchAction {private ItermsManager manager;public void setManager(ItermsManager manager) {
this.manager = manager;
}public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
System.out.println("进来了没?");
// 两种情况
DynaValidatorActionForm f = (DynaValidatorActionForm)form;
String id = request.getParameter("id");Iterms i = null;
if (id == null) { // 新增的一个物料
i = new Iterms();
}else { // 修改的已有的物料 ,根据id读出来这条记录
i = manager.getIterms(id);
}i.setCategory((String)f.get("category"));
i.setItemName((String)f.get("itermName"));
i.setPattern((String)f.get("pattern"));
i.setSpec((String)f.get("spec"));manager.save(i);
return list(mapping, form, request, response);
}public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
//String id = request.getParameter("id");
manager.delete(request.getParameter("id"));
return list(mapping, form, request, response);
}public ActionForward edit(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// 得到要修改记录的id值
String id = request.getParameter("id");
DynaValidatorActionForm f = (DynaValidatorActionForm)form;
// 得到要修改的这条记录
Iterms i = manager.getIterms(id);
// 对这条记录进行属性的修改,完成后存放到f对象中,各个属性的值来自于这条记录的属性
f.set("itermName", i.getItemName());
f.set("spec", i.getSpec());
f.set("pattern", i.getPattern());
f.set("category", i.getCategory());request.setAttribute("id", id);return mapping.findForward("modify");
}
public ActionForward list(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
List list = manager.getIterms();
request.setAttribute("list", list);
return mapping.findForward("list_success");
}public ActionForward unspecified(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
return list(mapping, form, request, response);
}
}jsp页面 <%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>增加物料页面</title><script type="text/javascript">
function addItem() {if (document.getElementById("itemName").value == "") {
alert("物料名称不能为空!");
document.getElementById("itemName").focus();
return;
}
with (document.getElementById("itermForm")) {
method = "post";
action = "item.do?command=add";
submit();
}
}
function goBack() {
window.self.location = "item.do?command=list";
}</script>
</head>
<body>
<form name="itermForm" target="_self" id="itermForm"><table>
<tr>
<td height="26">物料名称:</td>
<td><input name="itemName" type="text" id="itemName" size="20" maxlength="20"></td>
</tr>
<tr>
<td height="26">物料规格:</td>
<td><label>
<input name="spec" type="text" id="spec" size="20" maxlength="20">
</label></td>
</tr>
<tr>
<td height="26">型 号:</td>
<td><input name="pattern" type="text" id="pattern" size="20" maxlength="20"></td>
</tr>
</table><hr width="97%" align="center" size=0> <input name="btnAdd" type="button" value="添加" id="btnAdd" onClick="addItem();" >
<input name="btnBack" type="button" value="返回" id="btnBack" onClick="goBack();">
</form>
</body>
</html>
web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 通过应用上下文参数来指定spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param><filter>
<filter-name>encoding</filter-name>
<filter-class>com.zjl.jc.service.util.FilterEncoding</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping><!-- 配置一个加载Spring应用上下文的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
你可以
window.location = "/虚拟目录名字/item.do?command=list";采用绝对路径的方法