0

我正在从书中弹簧食谱中制作一个示例,并且我似乎无法将示例部署到 tomcat 中。我正在尝试打开 messageList.htm 页面,但我总是收到 HTTP 状态 404 错误。

有人可以帮助我吗?

提前谢谢

这是代码:

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 ">
    <:context-param>
        <:param-name>contextConfigLocation</:param-name>
        <:param-value>/WEB-INF/board-service.xml</:param-value>
    </:context-param>

    <:listener>
    <:listener-class>org.springframework.web.context.ContextLoaderListener</:listener-class>
    </:listener>

    <:servlet>
    <:servlet-name>Chapter5Security</:servlet-name>
    <:servlet-class>org.springframework.web.servlet.DispatcherServlet</:servlet-class>
    </:servlet>

    <:servlet-mapping>
    <:servlet-name>Chapter5Security</:servlet-name>
    <:url-pattern>/*</:url-pattern>
    </:servlet-mapping>
</:web-app>

板服务.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
    <bean id="messageBoardService" class="service.MessageBoardServiceImpl" />

</beans>

板-servlet.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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="web"></context:component-scan>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

消息列表控制器

@Controller
@RequestMapping("/messageList*")
public class MessageListController {

    private MessageBoardService messageBoardService;

    @Autowired
    public MessageListController(MessageBoardService messageBoardService)
    {
        this.messageBoardService=messageBoardService;
    }

    @RequestMapping(method= RequestMethod.GET)
    public String generateList(Model model)
    {
        List<Message>messages=java.util.Collections.emptyList();
        messages=messageBoardService.ListMessages();
        model.addAttribute("messages",messages);
        return "messageList";
    }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>be.glennvdv.BookSpring</groupId>
  <artifactId>Chapter5Security</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Chapter5Security Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.10</version>
          <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>3.1.2.RELEASE</version>
</dependency>
            <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>3.1.2.RELEASE</version>
</dependency>
            <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>3.1.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>3.1.2.RELEASE</version>
</dependency>

        <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.1.2.RELEASE</version>
</dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
        <version>3.1.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-core</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
            <dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-ldap</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
            <dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
            <dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
            <dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-taglibs</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
            <dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-acl</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>3.1.2.RELEASE</version>
</dependency>

  </dependencies>
    <build>
    <finalName>Chapter5Security</finalName>
    <plugins>
        <plugin>

            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <configuration>
            <server>myserver</server>

            </configuration>
        </plugin>
    </plugins>
  </build>
</project>

消息列表.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<html>
<head>
<title>Message List</title>
</head>

<body>

<c:forEach items="${messages}" var="message">
<table>
  <tr>
    <td>Author</td>
    <td>${message.author}</td>
  </tr>
  <tr>
    <td>Title</td>
    <td>${message.title}</td>
  </tr>
  <tr>
    <td>Body</td>
    <td>${message.body}</td>
  </tr>
  <tr>
    <td colspan="2">
      <a href="messageDelete?messageId=${message.id}">Delete</a>
    </td>
  </tr>
</table>
<hr />
</c:forEach>
<a href="messagePost">Post</a>
</body>
</html>
4

2 回答 2

2

HTTP 404 意味着您在浏览器中提供的 URL 没有映射到您希望的页面。

如果您将应用程序作为名为 foo.war 的 WAR 文件部署到 Tomcat,我希望 URL 如下所示:

http://localhost:8080/foo/messageList.html

使用该语法,直到正确为止。

如果 Maven 对您来说是一个黑匣子,我建议您手动创建和部署一个 WAR 文件,直到您弄明白为止。

于 2012-07-17T16:03:09.763 回答
0

得到它的工作。1:web.xml 格式不正确 2:Needet jstldependency

于 2012-07-18T10:22:48.423 回答