我在 Eclipse 中创建了一个小“动态 Web 项目”,并尝试显示/编辑/创建和删除我的数据库对象。Tomcat 服务器运行。但我收到 HTTP 状态 404 错误:请求的资源 (/TestDynWeb/) 不可用(http://localhost:8010/TestDynWeb)。我想显示我所有的代码文件。也许你知道出了什么问题。
网页内容/WEB-INF:
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>TestDynWeb</display-name>
<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WebContent/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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 Bean Definitions -->
<form-beans>
<form-bean name="ListForm" type="list.forms.ListForm"/>
</form-beans>
<!-- Action Mapping Definitions -->
<action-mappings>
<action path="/showlist" type="list.misc.ShowList" validate="false" scope="session">
<forward name="success" path="/jsp/listScreen.jsp"/>
</action>
<!-- Example of Struts Dispatch Action : has the extra attribute parameter-->
<action path="/listaction" type="list.actions.ListAction" parameter="actionMethod" name="ListForm"
validate="false" scope="session">
<forward name="addObject" path="/jsp/createScreen.jsp"/>
<forward name="editObject" path="/jsp/editScreen.jsp"/>
</action>
</action-mappings>
</struts-config>
网页内容/jsp:
listScreen.jsp
<%@ page import="java.util.HashMap"%>
<%@ page import="java.util.Map"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.Iterator"%>
<html>
<body>
<b>Available Data Base Objects</b>
<form name="listform" action="/listaction.do">
<table style="background-color: #82CAFA;">
<tr style="color: white;">
<th> </th>
<th>Object ID</th>
<th>Object Name</th>
<th>Person ID</th>
<th>Date (Dev)</th>
<th>Version (Dev)</th>
</tr>
<%
List objectsList = (ArrayList) request.getAttribute("objectsList");
Iterator itr = objectsList.iterator();
while (itr.hasNext()) {
Map map = (HashMap) itr.next();
%>
<tr>
<td><input type="radio" name="dboId"
value='<%=map.get("dboId")%>'
onclick="javascript:enableEditDelete();"></td>
<td><%=map.get("dboID")%></td>
<td><%=map.get("dboDevName")%></td>
<td><%=map.get("personId")%></td>
<td><%=map.get("changedByTimestampDEV")%></td>
<td><%=map.get("releaseVersionDev")%></td>
</tr>
<%
}
%>
</table>
</p>
<p>
<table>
<tr>
<td><input type="submit" name="actionMethod" id="editbutton"
value="Edit Object" disabled="true" style="color: black;" /></td>
<td><input type="submit" name="actionMethod" id="deletebutton"
value="Delete Object" disabled="true" onclick="return checkDelete();" style="color: black;" /></td>
</tr>
</table>
</form>
</p>
<script>
function checkDelete() {
return confirm("Are u sure to delete this object..?");
}
function enableEditDelete() {
document.getElementById('editbutton').disabled = false;
document.getElementById('deletebutton').disabled = false;
}
</script>
</body>
</html>
编辑屏幕.jsp
<b>Edit Object</b>
<html:form>
<p>
</p><table style="background-color: #82CAFA;">
<tr style="color: white;">
<td>Object ID</td>
<td><html:text property="dboId" disabled="true"/></td>
</tr>
<tr style="color: white;">
<td>Object Name</td>
<td><html:text property="dboDevName"/></td>
</tr>
<tr style="color: white;">
<td>Person ID</td>
<td><html:text property="personId"/></td>
</tr>
<tr style="color: white;">
<td>Date (Dev)</td>
<td><html:text property="changedByTimestampDEV"/></td>
</tr>
<tr style="color: white;">
<td>Version (Dev</td>
<td><html:text property="releaseVersionDev"/></td>
</tr>
</table>
<p/>
<p>
</p><table>
<tr>
<td><input type="submit" name="actionMethod" value="Save Object" style="color: black;"/></td>
</tr>
</table>
</html:form>
<p/>
创建屏幕.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html>
<body>
<b>Add Object</b>
<html:form>
<table style="background-color: #82CAFA;">
<tr>
<td>Object ID</td>
<td><html:text property="dboId" disabled="true" /></td>
</tr>
<tr>
<td>Object Name</td>
<td><html:text property="dboDevName" /></td>
</tr>
<tr>
<td>Person ID</td>
<td><html:text property="personId" /></td>
</tr>
<tr>
<td>Date (Dev)</td>
<td><html:text property="changedByTimestampDEV" /></td>
</tr>
<tr>
<td>Version (Dev</td>
<td><html:text property="releaseVersionDev" /></td>
</tr>
</table>
</p>
<p>
<table>
<tr>
<td><input type="submit" name="actionMethod" value="Add Object" /></td>
</tr>
</table>
</html:form>
</p>
</body>
</html>
/配置:
appContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
<property name="url"
value="jdbc:jtds:sqlserver://localhost:1433;databaseName=NARTrackingDB" />
<property name="username" value="abc" />
<property name="password" value="xyz" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="objDao" class="tracking.dao.DboDao">
<property name="transactionManager" ref="transactionManager" />
</bean>
</beans>
DAO类:
package tracking.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import javax.sql.DataSource;
import org.objectweb.asm.Type;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import tracking.bean.DboBean;
public class DboDao {
private DataSourceTransactionManager transactionManager;
private JdbcTemplate jdbcTemplate;
private DataSource dataSource; // either use @Autowire or provide xml config
//private String dboDevName;
public DboDao() {
// TODO Auto-generated constructor stub
super();
}
public DboBean read(final int dboId) {//String dboId
// TODO Auto-generated method stub
String sql = "SELECT * FROM da_tracking "
+ "WHERE ins_ID = " + dboId;
final Object[] params = new Object[] {};
//Creating object being queried
final DboBean dboBean = new DboBean();
//Process query Results
jdbcTemplate.query(sql, params, new RowCallbackHandler(){
public void processRow(ResultSet rs) throws SQLException{
dboBean.setDboId(rs.getString("ins_ID"));
dboBean.setDboDevName(rs.getString("ins_name"));
}
});
return dboBean;//returns queried objects
}
public int save(DboBean record ) {
// TODO Auto-generated method stub
String sql = "UPDATE da_tracking"
+ " SET ins_name= ?,"
+ " ins_dev_scripted = ?"
+ " WHERE ins_ID = ?" ;
Object[] params = new Object[] {
record.getDboDevName(),
record.getDevScripted(),
record.getDboId()
};
int[] types = new int[]{
Types.VARCHAR,
Types.BIT,
Types.INTEGER};
return jdbcTemplate.update(sql, params, types);
}
public int createInstance(DboBean record) {
// TODO Auto-generated method stub
String sql = "INSERT INTO da_tracking"
+ "(ins_ID,ins_name,ins_deployed_to_uat, ins_dev_status,ins_dev_scripted,ins_dev_changed_by_timestamp"
+ "change_details,db_owner_ID,type_ID,person_ID,changeType_ID,ins_uat_changed_by_timestamp,release_version_dev"
+ "release_version_uat) VALUES"
+ "( ?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
Object[] params = new Object[] {
record.getDboId(),record.getDboDevName(),record.getDeployedToUAT(),record.getDevStatus(),record.getDevScripted(),
record.getChangedByTimestampDEV(),record.getChangeDetails(),record.getDbOwner(),record.getTypeId(),record.getPersonId(),
record.getChangedByTimestampUAT(),record.getReleaseVersionDev(),record.getReleaseVersionUat()
};
int[] types = new int[]{
Types.INTEGER, Types.VARCHAR, Types.BIT, Types.INTEGER, Types.BIT,
Types.TIMESTAMP, Types.VARCHAR, Types.INTEGER, Types.INTEGER, Types.INTEGER,
Types.TIMESTAMP, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR
};
return jdbcTemplate.update(sql, params, types);
}
public void delete(String dboId) {
// TODO Auto-generated method stub
String sql = "DELETE FROM da_tracking "
+ "WHERE ins_ID = " + dboId;
Object[] params = new Object[] {};
//Process query Results
jdbcTemplate.update(sql, params);
}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
//Spring setter injector used
public void setTransactionManager(DataSourceTransactionManager transactionManager){
this.transactionManager = transactionManager;
DataSource datatSource = transactionManager.getDataSource();
jdbcTemplate = new JdbcTemplate(datatSource);
}
}
--DBOBean.java 包括 getter 和 setter--不需要在这里显示
显示列表.java:
package list.misc;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ShowList extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
System.out.println("Show Objects List");
ListObjects l = ListObjects.getInstance();
request.setAttribute("objectsList", l.getObjectList());
return mapping.findForward("success");
}
}
列表对象:
package list.misc;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
public class ListObjects {
int objectIdCount = 1000;
Map<Integer, StoreObject> objectMap = new HashMap<Integer, StoreObject>();
private static ListObjects objects = null;
private ListObjects() {
}
public static ListObjects getInstance() {
if (objects == null) {
objects = new ListObjects();
}
return objects;
}
public void storeObject(String dboDevName, int personId, String changedByTimestampDEV, String releaseVersionDev) {
StoreObject so = new StoreObject();
objectIdCount++;
so.addObject(objectIdCount, dboDevName, personId, changedByTimestampDEV, releaseVersionDev);
objectMap.put(objectIdCount, so);
}
public void updateObject(int dboId, String dboDevName, int personId, String changedByTimestampDEV, String releaseVersionDev) {
StoreObject so = objectMap.get(dboId);
so.updateObject(dboId, dboDevName, personId, changedByTimestampDEV, releaseVersionDev );
}
public Map searchObject(int dboId) {
return objectMap.get(dboId).getObject();
}
public void deleteObject(int dboIds) {
objectMap.remove(dboIds);
}
// Inner Class used to persist the app data ie. object details.
public class StoreObject {
private String dboDevName;
private String changeDetails;
private String changedByTimestampDEV;
String releaseVersionDev;
private int personId;
private int objectIdCount;
private int dboId;
StoreObject() {
}
public void addObject(int objectIdCount, String dboDevName, int personId, String changedByTimestampDEV, String releaseVersionDev) {
this.objectIdCount = objectIdCount;
this.dboDevName = dboDevName;
this.personId = personId;
this.changedByTimestampDEV = changedByTimestampDEV;
this.releaseVersionDev = releaseVersionDev;
}
public void updateObject(int dboId, String dboDevName, int personId, String changedByTimestampDEV, String releaseVersionDev) {
this.dboId = dboId;
this.dboDevName = dboDevName;
this.personId = personId;
this.changedByTimestampDEV = changedByTimestampDEV;
this.releaseVersionDev = releaseVersionDev;
}
@SuppressWarnings("unchecked")
public Map getObject() {
Map objects = new HashMap();
objects.put("objectIdCount", this.objectIdCount);
objects.put("dboDevName", this.dboDevName);
objects.put("personId", this.personId);
objects.put("changedByTimestampDEV", this.changedByTimestampDEV);
objects.put("releaseVersionDev", this.releaseVersionDev);
return objects;
}
}
public List getObjectList() {
List objectsList = new ArrayList();
Set s = objectMap.keySet();
Iterator itr = s.iterator();
while (itr.hasNext()) {
objectsList.add(objectMap.get((Integer) itr.next()).getObject());
}
return objectsList;
}
}
--ListForm.java 包括 getter & setter -- 这里不需要。
ListAction.java:
package list.actions;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.actions.DispatchAction;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import list.forms.ListForm;
import list.misc.ListObjects;
public class ListAction extends DispatchAction {
public ActionForward AddObj(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
System.out.println("Add Object Page");
return mapping.findForward("addObj");
}
public ActionForward EditObj(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
System.out.println("Edit Object Page");
int dboId = Integer.parseInt(request.getParameter("dboId"));
ListObjects l = ListObjects.getInstance();
@SuppressWarnings("rawtypes")
Map objMap = l.searchObject(dboId);
// Used form bean class methods to fill the form input elements with
// selected object values.
ListForm lf = (ListForm) form;
lf.setDboId((Integer) objMap.get("dboId"));
lf.setDboDevName(objMap.get("dboDevName").toString());
lf.setTypeId((Integer) objMap.get("typeId"));
lf.setChangeDetails(objMap.get("changeDetails").toString());
lf.setChangeType((Integer) objMap.get("changeType"));
lf.setDevScripted((Boolean) objMap.get("devScripted"));
lf.setPersonId((Integer) objMap.get("personId"));
lf.setChangedByTimestampDEV(objMap.get("changedByTimestampDEV").toString());
lf.setChangedByTimestampUAT(objMap.get("changedByTimestampUAT").toString());
lf.setReleaseVersionDev(objMap.get("releaseVersionDev").toString());
lf.setReleaseVersionUat(objMap.get("releaseVersionUat").toString());
return mapping.findForward("addObject");
}
public ActionForward SaveObj(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
System.out.println("Save Object");
// Used form bean class methods to get the value of form input elements.
ListForm lf = (ListForm) form;
int dbOwner = lf.getDbOwner();
String dboDevName = lf.getDboDevName();
int typeId = lf.getTypeId();
String changeDetails = lf.getChangeDetails();
int changeType = lf.getChangeType();
Boolean devScripted = lf.isDevScripted();
int personId = lf.getPersonId();
String changedByTimestampDEV = lf.getChangedByTimestampDEV();
String changedByTimestampUAT = lf.getChangedByTimestampUAT();
String releaseVersionDev = lf.getReleaseVersionDev();
String releaseVersionUat = lf.getReleaseVersionUat();
ListObjects l = ListObjects.getInstance();
l.storeObject(dboDevName, personId, changedByTimestampDEV, releaseVersionDev);
return new ActionForward("/showlist.do", true);
}
public ActionForward CreateObj(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
System.out.println("Update Object");
ListForm lf = (ListForm) form;
int dboId = lf.getDboId();
String dboDevName = lf.getDboDevName();
int typeId = lf.getTypeId();
String changeDetails = lf.getChangeDetails();
int changeType = lf.getChangeType();
Boolean devScripted = lf.isDevScripted();
int personId = lf.getPersonId();
String changedByTimestampDEV = lf.getChangedByTimestampDEV();
String changedByTimestampUAT = lf.getChangedByTimestampUAT();
String releaseVersionDev = lf.getReleaseVersionDev();
String releaseVersionUat = lf.getReleaseVersionUat();
ListObjects l = ListObjects.getInstance();
l.updateObject(dboId, dboDevName, personId, changedByTimestampDEV, releaseVersionDev);
return new ActionForward("/showlist.do", true);
}
public ActionForward DeleteObject(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
System.out.println("Delete Object");
int dboId = Integer.parseInt(request.getParameter("dboId"));
ListObjects lo = ListObjects.getInstance();
lo.deleteObject(dboId);
return new ActionForward("/showlist.do", true);
}
}
非常感谢您的每一次帮助。