您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“java中怎么用注解方式進行配置頁面跳轉(zhuǎn)”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
1、修改IndexController
在類前面加上@Controller 表示該類是一個控制器
在方法handleRequest 前面加上 @RequestMapping("/index") 表示路徑/index會映射到該方法上
注意:不再讓IndexController實現(xiàn)Controller接口
@Controller public class IndexController { @RequestMapping("/index") public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mav = new ModelAndView("index"); mav.addObject("message", "Hello Spring MVC"); return mav; } }
2、修改springmvc-servlet.xml
去掉映射相關(guān)的配置,因為已經(jīng)使用注解方式了
增加<context:component-scan base-package="controller" />
<?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" 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="controller" /> <bean id="irViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/page/" /> <property name="suffix" value=".jsp" /> </bean> <!-- <bean id="simpleUrlHandlerMapping" --> <!-- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> --> <!-- <property name="mappings"> --> <!-- <props> --> <!-- <prop key="/index">indexController</prop> --> <!-- </props> --> <!-- </property> --> <!-- </bean> --> <!-- <bean id="indexController" class="controller.IndexController"></bean> --> </beans>
“java中怎么用注解方式進行配置頁面跳轉(zhuǎn)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。