struts2写的一个程序,怎么就是访问不了页面呢,路径啊路径啊,求解……

logoyw 发布于 2012/10/11 18:42
阅读 1K+
收藏 0
 


一个简单的struts2_crud操作,jar包已导入,加载到tomcat后,总是访问出现404错误,路径究竟错哪了啊,,求教啊啊

1.运行环境  MyEclipse6.0+自带Tomcat

2.源代码:

   三个java类

   ---->Book.java

        public class Book {

 private String isbn;
 private String title;
 private double price;

 public Book() {
 }

 public Book(String isbn, String title, double price) {
  this.isbn = isbn;
  this.title = title;
  this.price = price;
 }

 public String getIsbn() {
  return isbn;
 }

 public void setIsbn(String isbn) {
  this.isbn = isbn;
 }

 public double getPrice() {
  return price;
 }

 public void setPrice(double price) {
  this.price = price;
 }

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }
}

    --->BookAction.java

import java.util.Collection;
import com.opensymphony.xwork2.ActionSupport;

public class BookAction extends ActionSupport{

 private static final long serialVersionUID = 872316812305356L;
 private String isbn;
 private String[] isbns;
 private Book book;
 private Collection<Book> books;
 private BookDao dao = BookDao.getInstance();

 public Book getBook() {
  return book;
 }

 public void setBook(Book book) {
  this.book = book;
 }

 public String getIsbn() {
  return isbn;
 }

 public void setIsbn(String isbn) {
  this.isbn = isbn;
 }

 public String[] getIsbns() {
  return isbns;
 }

 public void setIsbns(String[] isbns) {
  this.isbns = isbns;
 }

 public Collection<Book> getBooks() {
  return books;
 }

 public void setBooks(Collection<Book> books) {
  this.books = books;
 }

 public String load() {
  book = dao.getBook(isbn);
  return SUCCESS;
 }

 public String list() {
  books = dao.getBooks();
  return SUCCESS;
 }

 public String store() {
  dao.storeBook(book);
  return SUCCESS;
 }

 public String remove() {
  if (null != isbn) {
   dao.removeBook(isbn);
  } else {
   dao.removeBooks(isbns);
  }
  return SUCCESS;
 }
}
    --->BookDao.java

import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class BookDao {

 private static final BookDao instance;
 private static final ConcurrentMap<String, Book> data;
 static {
  instance = new BookDao();
  data = new ConcurrentHashMap<String, Book>();
  data.put("978-0735619678", new Book("978-0735619678", "Code Web site",
    32.99));
  data.put("978-0596007867", new Book("978-0596007867", "EJB", 35.96));
  data.put("978-0201633610", new Book("978-0201633610",
    "Design Patterns", 43.19));
  data.put("978-0596527341", new Book("978-0596527341",
    "Information Architectures", 25.19));
  data.put("978-0735605350", new Book("978-0735605350", "Black Art",
    25.19));
 }

 private BookDao() {
 }

 public static BookDao getInstance() {
  return instance;
 }

 public Collection<Book> getBooks() {
  return data.values();
 }

 public Book getBook(String isbn) {
  return data.get(isbn);
 }

 public void storeBook(Book book) {
  data.put(book.getIsbn(), book);
 }

 public void removeBook(String isbn) {
  data.remove(isbn);
 }

 public void removeBooks(String[] isbns) {
  for (String isbn : isbns) {
   data.remove(isbn);
  }
 }
}

  struts.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.devMode" value="true"/>
 <package name="struts2_crud" extends="struts-default" namespace="/book">
  <action name="List" class="org.xmh.demo.BookAction"
   method="list">
   <result>/List.jsp</result>
  </action>
  <action name="Edit" class="org.xmh.demo.BookAction"
   method="load">
   <result>/Edit.jsp</result>
  </action>
  <action name="Store" class="org.xmh.demo.BookAction"
   method="store">
   <result type="redirect">/List.action</result>
  </action>
  <action name="Remove" class="org.xmh.demo.BookAction"
   method="remove">
   <result type="redirect">/List.action</result>
  </action>
 </package>
</struts>

 web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>

 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  </filter-class>
 </filter>

 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>

两个jsp页面

 List.jsp-->

<%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme() + "://"
   + request.getServerName() + ":" + request.getServerPort()
   + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <base href="<%=basePath%>" />
  <title>Book List</title>
  <style type="text/css">
table {
 border: 1px solid black;
 border-collapse: collapse;
}

table thead tr th {
 border: 1px solid black;
 padding: 3px;
 background-color: #cccccc;
}

table tbody tr td {
 border: 1px solid black;
 padding: 3px;
}
</style>
 </head>
 <body>
  <h2>
   Book List
  </h2>
  <s:form action="Remove" theme="simple">
   <table cellspacing="0">
    <thead>
     <tr>
      <th>
       Select
      </th>
      <th>
       ISBN
      </th>
      <th>
       Title
      </th>
      <th>
       Price
      </th>
      <th>
       Operation
      </th>
     </tr>
    </thead>
    <tbody>
     <s:iterator value="books">
      <tr>
       <td>
        <input type="checkbox" name="isbns"
         value='<s:property value="isbn" />' />
       </td>
       <td>
        <s:property value="isbn" />
       </td>
       <td>
        <s:property value="title" />
       </td>
       <td>
        $
        <s:property value="price" />
       </td>
       <td>
        <a
         href='<s:url action="Edit"><s:param name="isbn" value="isbn" /></s:url>'>
         Edit </a> &nbsp;
        <a
         href='<s:url action="Remove"><s:param name="isbn" value="isbn" /></s:url>'>
         Delete </a>
       </td>
      </tr>
     </s:iterator>
    </tbody>
   </table>
   <s:submit value="Remove" />
   <a href="Edit.jsp">Add Book</a>
  </s:form>
 </body>
</html>

      Edit.jsp-->

<%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title>Book</title>
 </head>
 <body>
  <h2>
   <s:if test="null == book">
Add Book
</s:if>
   <s:else>
Edit Book
</s:else>
  </h2>
  <s:form action="Store">
   <s:textfield name="book.isbn" label="ISBN" />
   <s:textfield name="book.title" label="Title" />
   <s:textfield name="book.price" label="Price" />
   <s:submit />
  </s:form>
 </body>
</html>

 

以下是问题补充:

@logoyw:問題已經解決了﹐原來發現是jar包的問題﹐在官網上下了struts2的新jar包﹐發現ok..-->正常了... (2012/10/15 10:24)
加载中
0
业余编程人士
业余编程人士
看花眼啊.....
0
JustForFly
JustForFly

所有的

<s:form action="">

url前面加上: /book

你的struts.xml里面配置了 namespace="/book"


0
JFinal
JFinal
仅仅一个 CRUD,代码量也太大了吧   
l
logoyw
是哎﹐情何以堪
0
JFinal
JFinal
把错误提示贴出来吧
0
l
logoyw

今天再次試了一下﹐發現tomcat啟動有問題,,這個真是傷不起啊^^

嚴重的: Exception starting filter struts2

.....

Caused by: Unable to load jar:file:/D:/workspace-spring/.metadata/.plugins/com.genuitec.eclipse.easie.tomcat.myeclipse/tomcat/webapps/struts2_crud/WEB-INF/lib/struts2-core-2.1.6.jar!/struts-default.xml - [unknown location]

返回顶部
顶部