国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

java - Spring MVC無法識別Controller導(dǎo)致返回的結(jié)果是404?
阿神
阿神 2017-04-18 10:55:44
0
6
552

運(yùn)行環(huán)境

java8 + idea2016 + spring mvc4.3 + maven3.3 + tomcat8 + ubuntu

報(bào)錯(cuò)信息

訪問localhost:8080/HelloWeb/hello或者localhost:8080/hello都會返回404的狀態(tài)碼:

具體配置

項(xiàng)目結(jié)構(gòu)

Web.xml

<web-app id="WebApp_ID" 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">

    <display-name>Spring MVC Form Handling</display-name>

    <servlet>
        <servlet-name>HelloWeb</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloWeb</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

HelloWeb-servlet.xml

<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 base-package="org.neo.sml"/>

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

</beans>

HelloController

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

    @RequestMapping(value="/hello")
    public String printHello(ModelMap model) {
        model.addAttribute("message", "Hello Spring MVC Framework!");
        return "hello";
    }
}

hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>

補(bǔ)充

我猜測原因是spring mvc沒有識別@Controller注解,但是我在HelloWeb-servlet.xml中已經(jīng)聲明了<context:component-scan base-package="org.neo.sml"/>,請問發(fā)生錯(cuò)誤的原因可能是什么?

按照@傅易君的提示,參考so上面,我加入了<mvc:annotation-driven />,但是還是不行:

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

    <context:component-scan base-package="org.ziwenxie.sml"/>
    <mvc:annotation-driven/>

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

</beans>
阿神
阿神

閉關(guān)修行中......

reply all(6)
巴扎黑

Put HelloWeb-servlet.xml into resources and change web.xml to
<servlet>

         <servlet-name>DispatcherServlet</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         <init-param>
                 <param-name>contextConfigLocation</param-name>
                 <param-value>classpath:HelloWeb-servlet.xml</param-value>
         </init-param>
 </servlet>

The configuration HelloWeb-servlet.xml declares <context:component-scan base-package="org.neo.sml"/>
The access path is just http://localhost:8080/sml/hello

伊謝爾倫

Power off the controller and see if the controller is not connected?

洪濤

Try localhost:8080/sml

黃舟

Enable annotation support first:

<mvc:annotation-driven />
伊謝爾倫

There are mainly the following aspects to pay attention to:
1. Whether to configure the location of the xml file in the web.

<servlet>
      <servlet-name>springmvc</servlet-name>      
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:HelloWeb-servlet.xml</param-value>
      </init-param>  
  </servlet>

2. Is it configured in the HelloWeb-servlet.xml file, as shown below:

<mvc:annotation-driven></mvc:annotation-driven>        
    <context:component-scan base-package="cn"></context:component-scan>                
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsps/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

3. Is the directory in which the project is started correct? Do you need to add the project name?

4. After startup, is the directory correct?

左手右手慢動作

Accessing localhost:8080/HelloWeb/hello or localhost:8080/hello will return a 404 status code

I think you didn’t lose this path, right? Why is HelloWeb而不是sml?一般不是項(xiàng)目的名稱么,默認(rèn)是。
或者,你找到tomcat的server.xml? Find the following configuration.

<Context docBase="test" path="/test" reloadable="true" source="org.eclipse.jst.jee.server:test"/>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template