【问题标题】:Hello Program in Spring MVCSpring MVC 中的 Hello 程序
【发布时间】:2016-05-24 06:20:28
【问题描述】:

运行以下代码时出现 404 错误,请帮助。这里一切正常,但我仍然收到 404 错误。请帮我解决这个问题。

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" 
    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>Hello Spring</display-name>

          <servlet>
          <servlet-name>hello</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <load-on-startup>1</load-on-startup>  
          </servlet>
          <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
            <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
          </welcome-file-list>
          </web-app><br/>

你好-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan basepackage="com.hello"/>
<bean class="org.springframework.web.servlet.view.InternalResouceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp"/>
</bean>
</beans></br>

你好控制器

@Controller
@RequestMapping
public class HelloController {
    @RequestMapping("/hello")
    public ModelAndView getHello()
    {
        return new ModelAndView("hellopage","hellomessage","Welcome Spring MVC!!!");
    }

}</br>

你好页面

<html>
<body>
<h1>${hellomessage}</h1>
</body>
</html>

【问题讨论】:

  • 你用的是哪个版本的spring jar??
  • 你的项目名和控制器类包名是什么??
  • 在 Web 上找到 tutorialsdesk.com/2016/01/… 示例,用于 Spring MVC Hello World 示例
  • HelloMVC 是我的项目名称 & HelloController 是控制器,com.hello 是我的包。4.0.6 是我的 jar 版本
  • 所以你的 URL 应该是 HelloMVC/hello,你是不是也一样?

标签: java spring model-view-controller


【解决方案1】:

如果你从 Spring-Boot 开始呢?您可以毫不费力地从 Spring 开始,它将从头开始为您构建和配置一切。这是您要查找的示例的起点:

http://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-first-application.html

您还可以下载 Spring Tool Suite 并从 Example 创建一个新项目以了解如何执行此操作。那里有很多不错的教程:)

希望对你有帮助!

问候

【讨论】:

    【解决方案2】:

    对于 Spring 4.x.x ,无需在 dispatcher-servlet 配置的 &lt;beans xsi:schemaLocation""&gt; 中提及版本(在您的情况下为 hello-servlet.xml),请按照以下说明进行更新。

    <beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
     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.xsd
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd">
    
     <context:component-scan basepackage="com.hello"/>
    
     <bean class="org.springframework.web.servlet.view.InternalResouceViewResolver">
          <property name="prefix" value="/WEB-INF/jsp/" />
          <property name="suffix" value=".jsp"/>
     </bean>
     </beans>
    

    【讨论】:

    • 你在WEB-INF/lib文件夹下放了jar吗?
    • 又不是InternalViewResolver,改成InternalResouceViewResolver
    • 我无法将 jar 移动到 webinf 文件夹 lib,请帮助
    • 你在用eclipse吗??如果是这样,从文件资源管理器打开工作区,然后将 jar 复制并粘贴到 lib 文件夹
    • 关注这个tutorialsdesk.com/2016/01/…有一步一步的解决方案,我希望它对你有用
    猜你喜欢
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 2015-03-25
    • 2017-10-11
    • 2012-12-31
    相关资源
    最近更新 更多