简单的Spring MVC 程序

本文详细介绍了 Spring MVC 框架中一个名为 EmpAction 的控制器类,该类负责处理与员工相关的请求。文章展示了如何使用 @RequestMapping 注解来指定请求的映射路径,并演示了如何通过 @RequestParam 获取请求参数、如何处理 Servlet 内置对象等。此外,还提供了日志记录和异常处理的相关代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package group.esperanto.action;

import java.io.IOException;

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

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
// 定义该Action的映射路径
// 该路径绝对不能重复
@RequestMapping("/pages/emp/*")
public class EmpAction {	
	private	Logger log = Logger.getLogger(EmpAction.class);
	
	@RequestMapping("echo")  //映射方法名
   public void echo(String msg){
	   Logger.getLogger(EmpAction.class).info(msg);
   }
	@RequestMapping("list")
   public ModelAndView list(    // 设置参数亲求名和 默认值
			   @RequestParam(value="cp",defaultValue="1") int currentPage,
			   @RequestParam(value="lc",defaultValue="10") int lineSize,
			   @RequestParam(value="col",defaultValue="title") String column,
			   @RequestParam(value="kw",defaultValue="") String keyWord){
		     log.info("--- currentPage : "+currentPage+" ---");
		     log.info("--- lineSize : "+lineSize+" ---");
		     log.info("--- column : "+column+" ---");
		     log.info("--- keyWord : "+keyWord+" ---");		
		     return null;
	 }
	@RequestMapping("get")    // 接收Servlet内置对象 request,response
   public ModelAndView get(int id,HttpServletRequest request,HttpServletResponse response){
	   HttpSession session = request.getSession();
	   ServletContext application = request.getServletContext();
	   log.info("--- contextPath :" + request.getServletPath());
	   log.info("--- sessionId :" + session.getId());
	   log.info("--- realPath :" + application.getRealPath("/"));
	   try {
		    response.getWriter().println("Hello Spring MVC");
	    } catch (IOException e) {
		    e.printStackTrace();
	    }
	   return null;
   }	
}

 

转载于:https://my.oschina.net/u/3647851/blog/1507272

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值