在spring mvc中没有调用控制器方法

时间:2016-12-19 17:00:11

标签: java spring spring-mvc

我正在修改我在特定网站上找到的项目。 问题是当我在服务器上运行项目时,会显示包含美国地图的home.jsp页面。我已经使地图可以点击。当我点击地图状态时。生成的新URL是localhost:7001/SpringMVCAngularJS/home.jsp?statechoice=ME。根据spring mvc逻辑,应该调用getUSCity()方法。但它没有被召集。即使我只打localhost:7001/SpringMVCAngularJS/home.jsp,它也不会被召唤。 然而,如果我点击localhost:7001/SpringMVCAngularJS/springAngularJS.web,则调用特定方法。如果我错过任何映射,你能告诉我吗?

@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);

    }

}

我的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'>

分配器一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

<?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>

1 个答案:

答案 0 :(得分:0)

根据spring中的路径变量概念,它应该如下所示。你在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);

    }

你应该点击的url是localhost:7001 / SpringMVCAngularJS / home.web / ME,根据spring语法。其中ME是你在州选择中得到的值。

相关问题