您当前的位置: 首页 >  spring

星夜孤帆

暂无认证

  • 1浏览

    0关注

    626博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SpringMVC基础-02-使用注解

星夜孤帆 发布时间:2019-04-20 18:44:22 ,浏览量:1

配置pom.xml文件

  4.0.0
  com.monkey1024
  02mvc
  0.0.1-SNAPSHOT
  war
  
        
            junit
            junit
            3.8.1
            test
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
        
        
            org.springframework
            spring-webmvc
            5.0.4.RELEASE
        

    
    
    	
        02mvc
        
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    
                    1.8
                    1.8
                    UTF-8
                
            
        
    
配置web.xml


	 

	
	
		springMVC
		
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:springmvc.xml
		
		1
	
	
		springMVC
		/
	
配置springmvc.xml文件


        
	
	
	
	
	
	
	
	
    
	
	
	
	
	
	
		
		
	
	
	

 
Controller
package com.monkey1024.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


/*
 * 使用注解后就不需要实现接口了
 */
@Controller//表示当前类是一个controller
@RequestMapping("/test")//表示是一个命名空间namespace,抽取出重复的部分
public class HelloSpringMVC  {
	
	
	// 需要处理的url
//	@RequestMapping("/test/test2.do")
	@RequestMapping("/test2.do")
	// 方法名可以随意命名
	public ModelAndView test1(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView();
		// 类似键值对
		mv.addObject("hello","test2");
		// 连接到jsp
		mv.setViewName("first");
		return mv;
		
		
		
	}
	/*
	 * 以数组的形式,下面两个都可以调用这个方法
	 */
//	@RequestMapping({"/test/hello.do","/test/world.do"})
	@RequestMapping({"/hello.do","/world.do"})
	
	public ModelAndView test2(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView();
		// 类似键值对
		mv.addObject("hello","helloworld");
		// 连接到jsp
		mv.setViewName("first");
		return mv;
		
		
		
	}

}
jsp页面




Insert title here


${hello}

 

关注
打赏
1636984416
查看更多评论
立即登录/注册

微信扫码登录

0.0385s