Spring mvc controller class is not generated in [p

2019-07-22 14:38发布

i had created a simple spring-mvc maven project,but when i am trying to run that on server and requesting any page than it is showing me following error:


WARNING: No mapping found for HTTP request with URI [/spring-mvc3/outerlink.html] in DispatcherServlet with name 'spring'

but then i found that my project is not generating controller class in spring-mvc3-target-classes directory

This is my web.xml file

<?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">
    <display-name>spring-mvc3</display-name>
<welcome-file-list>index.jsp</welcome-file-list>
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

This is my spring-servlet.xml file:

<?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:tx="http://www.springframework.org/schema/tx" 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.0.xsd
                      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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="first"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"></property>
</bean>

</beans>

This is EmpController.java class which is present inside src/main/java first package:

package first;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class EmpController {

@RequestMapping("/outerlink")
public ModelAndView doWork(HttpServletRequest request)
{
    System.out.println("Entered in Model");
    return new ModelAndView("empform");
}
}

This is my Project Structure,my project name is spring-mvc3

enter image description here

Please help me i am badly stuck with this,thanks in advance

1条回答
劫难
2楼-- · 2019-07-22 15:07

I think your main problem is you are using html page, if you are using .html page then ModelAndView can't execute. for this you need to make that page as .jsp file.

If you want to use html file as it is then you need to redirect it. As html page is a static page you need to configure in your spring-servlet.xml as below

<mvc:resources mapping="/resources/**" location="/resources/" />

to redrect : return "redirect:resources/htmlPageName"

Spring xml file namespace should have spring mvc.

    <?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"
        xmlns:mvc="http://www.springframework.org/schema/mvc" 
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

        <mvc:annotation-driven />
        <context:component-scan base-package="first" />

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

    </beans>

put below jars and java version should java 8

enter image description here

查看更多
登录 后发表回答