简体   繁体   English

Spring MVC中未调用控制器方法

[英]Controller method not getting called in spring mvc

I am modifying on a project that I found on a particular site. 我正在修改在特定站点上找到的项目。 Issue is when I am running the project on server ,the home.jsp page gets displayed which contains a US map . 问题是当我在服务器上运行项目时,显示的home.jsp页面包含美国地图。 I have made the map clickable .When I am clicking on the map state .The new URL generated is localhost:7001/SpringMVCAngularJS/home.jsp?statechoice=ME .As per spring mvc logic , the getUSCity() method should be called . 我已经使地图可点击。当我点击地图状态时,生成的新URL为localhost:7001/SpringMVCAngularJS/home.jsp?statechoice=ME 。根据spring mvc逻辑,应调用getUSCity()方法。 But it is not getting called . 但是它没有被调用。 Even if I am hitting just localhost:7001/SpringMVCAngularJS/home.jsp ,its not getting called . 即使我只访问localhost:7001/SpringMVCAngularJS/home.jsp ,它也不会被调用。 Whereas if I hit localhost:7001/SpringMVCAngularJS/springAngularJS.web ,the particular method is called . 而如果我点击localhost:7001/SpringMVCAngularJS/springAngularJS.web ,则将调用特定方法。 Could you let me know if I am missing any mapping or so . 你能告诉我我是否缺少任何映射。

@Controller
public class SpringMVCController {

    @Autowired
    Top10LanesService lanesService;

    @RequestMapping(value = "/angularJS.web", method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        return "Home";
    }

    @RequestMapping(value = "/springAngularJS.web", method = RequestMethod.GET, produces = { "application/xml",
            "application/json" })
    public @ResponseBody List<LmWebShipment> getTop10Lanes() {

        List<LmWebShipment> al = new ArrayList<LmWebShipment>();

        // Top10LanesDAOImpl top10LanesDaoImpl = new Top10LanesDAOImpl();
        al = lanesService.getTop10Lanes();
        return al;
    }

    // Called on Click of US state

    @RequestMapping(value = "/home.web", method = RequestMethod.GET, produces = { "application/xml",
            "application/json" })
    @ResponseBody
    public void getUSCity(@PathVariable("statechoice") String statechoice) {

        System.out.println("In getUSCity" + statechoice);

    }

}

I have something like this in my home.jsp file . 我的home.jsp文件中有类似的内容。

    <area shape='polygon' coords='489,52,502,31,513,32,518,54,490,71' href='home.jsp?statechoice=ME' target="right"  alt='Maine'>

Dispatcher-servlet.xml Dispatcher-servlet.xml

<context:property-placeholder location="classpath:database/database.properties"/>
    <context:component-scan base-package="com.javahonk.controller" />
    <mvc:annotation-driven />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/Views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    <!-- bind messages.properties -->
    <bean class="org.springframework.context.support.ResourceBundleMessageSource"
        id="messageSource">
        <property name="basename" value="messages/messages" />
    </bean>

<bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean> 
    <bean id="top10LanesDAO" class="com.javahonk.DAO.Top10LanesDAOImpl" />
    <bean id="top10LanesService" class="com.javahonk.services.Top10LanesServiceImpl" />

</beans>

web.xml 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_2_5.xsd"
    version="2.5">
    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.web</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <welcome-file-list>
        <welcome-file>angularJS.web</welcome-file>
    </welcome-file-list>
    <display-name>SpringAngularJS</display-name>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

According to path variable concept in spring it should be as follows.you are missing the path variable in the url 根据春季的路径变量概念,应如下所示。您在url中缺少路径变量

  @RequestMapping(value = "/home.web/{statechoice}", method = RequestMethod.GET, produces = { "application/xml",
            "application/json" })
    @ResponseBody
    public void getUSCity(@PathVariable("statechoice") String statechoice) {

        System.out.println("In getUSCity" + statechoice);

    }

And the url you should hit is localhost:7001/SpringMVCAngularJS/home.web/ME according to the spring syntax.where ME is the value that you will get in state choice. 根据春季语法,您应该输入的URL是localhost:7001 / SpringMVCAngularJS / home.web / ME。其中ME是您将在状态选择中获得的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM