springMVC——注解配置方式实现Helloworld
基于注解helloworld
只需根據基于xml的項目文件進行改變。
改變1:springMVC.xml
<?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:p="http://www.springframework.org/schema/p"
???????xmlns:aop="http://www.springframework.org/schema/aop"
???????xmlns:context="http://www.springframework.org/schema/context"
???????xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
????<!--啟用classpath掃描-->
????<context:component-scan base-package="com.henu.controller"></context:component-scan>
????<!--映射處理器 -->
????<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
????<!--處理器適配器 -->
????<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
????<!--視圖解析器-->
????<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
????????<!--指定跳轉位置?/success.jsp-->
????????<property name="prefix" value="/"/>
????????<property name="suffix" value=".jsp"/>
????</bean>
</beans>
改變2:UserController
package com.henu.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.stereotype.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
?* @author George
?* @description
?**/
@Controller
public class UserController{
????/**
?????* 處理器方法
?????*
?????* @return
?????* @throws Exception
?????*/
????@RequestMapping("/login")
????public ModelAndView login() throws Exception {
????????//創建模型視圖對象
????????ModelAndView mav = new ModelAndView();
????????//把數據綁定到模型對象
????????mav.addObject("name","admin");
????????//設置跳轉的試圖對象
????????mav.setViewName("success");
????????return mav;
????}
}
然后啟動tomcat
不過路徑如果你是使用插件啟動的。需要看控制臺打出的路徑。
插件啟動位置:
?
控制臺打出的路徑:
?
因此:http://localhost:9091/mvc/login
?
總結
以上是生活随笔為你收集整理的springMVC——注解配置方式实现Helloworld的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springMVC——Xml配置方式实现
- 下一篇: 大剑无锋之浅析Cookie/Sessio