您当前的位置: 首页 >  spring

星夜孤帆

暂无认证

  • 1浏览

    0关注

    626博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SpringMVC基础-07-Controller中方法的返回值

星夜孤帆 发布时间:2019-04-21 15:12:03 ,浏览量: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


	 
	
	
	
		characterEncodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
		
			
			encoding
			utf-8
		
		
		
		
			forceEncoding
			true
		
		
	
	
		characterEncodingFilter
		/*
	
	
	
		springMVC
		
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:springmvc.xml
		
		1
	
	
		springMVC
		/
	
springmvc.xml


        
	
	
	
	
	
	
	
	
    
	
	
	
	
	
	
	
	
	
		
	
	
	
	
		
		
	
	

	
	

 
ReturnStringController.java
package com.monkey1024.controller;

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

/*
 * 方法中返回String类型
 * 如果你只希望方法执行完毕后跳转jsp或其他资源,此时可以使用String作为方法的返回值
 */
@Controller
public class ReturnStringController {
	
	/*
	 * 跳转到内部资源
	 */
	@RequestMapping("/welcome.do")
	public String welcome() throws Exception{
		return "welcome";
	}
	
	/*
	 * 跳转到外部资源
	 */
	@RequestMapping("/monkey1024.do")
	public String monkey1024() throws Exception{
		
		//此处返回的字符串需要跟springmvc.xml配置文件中外部资源view对象的bean的id保持一致
		return "monkey1024";
	}
}
ReturnVoidController.java
package com.monkey1024.controller;

import java.io.IOException;

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

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

import com.monkey1024.bean.Student;

@Controller
public class ReturnVoidController {
	
	/*
	 * 使用原生servlet中类
	 */
	@RequestMapping("/servletjump.do")
	public void servletJump(HttpServletRequest request,HttpServletResponse response, Student student) throws ServletException, IOException{
		request.setAttribute("student", student);
		request.getRequestDispatcher("/jsp/welcome.jsp").forward(request,response);
	}

}
welcome.jsp



Insert title here


	欢迎你来学习java
	${uername }
	${string }
	
	${student.name }
	${student.age }

 

 

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

微信扫码登录

0.0381s