controller类实现,只需把注解写上
来源:互联网 发布:淘宝女装店铺介绍范文 编辑:程序博客网 时间:2024/05/24 04:46
controller类实现,只需把注解写上,spring就会自动帮你找到相应的bean,相应的注解标记意义,不明白的,可以自己查下@Service,@Controller,@Entity等等的内容。
package com.mvc.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.mvc.entity.Student;
import com.mvc.service.StudentService;
@Controller
@RequestMapping( " /student.do " )
public class StudentController {
protected final transient Log log=LogFactory
.getLog(StudentController.class);
@Autowired
private StudentService studentService;
public StudentController(){
}
@RequestMapping
public String load(ModelMap modelMap){
Listlist=studentService.getStudentList();
modelMap.put("list", list);
return "student";
}
@RequestMapping(params="method=add")
public String add(HttpServletRequest request, ModelMap modelMap) throws Exception{
return "student_add";
}
@RequestMapping(params="method=save")
public String save(HttpServletRequest request, ModelMap modelMap){
String user=request.getParameter("user");
String psw=request.getParameter("psw");
Student st=new Student();
st.setUser(user);
st.setPsw(psw);
try{
studentService.save(st);
modelMap.put("addstate", "添加成功");
}
catch(Exception e){
log.error(e.getMessage());
modelMap.put("addstate", "添加失败");
}
return "student_add";
}
@RequestMapping(params="method=del")
public void del(@RequestParam("id") String id, HttpServletResponse response){
try{
Student st=new Student();
st.setId(Integer.valueOf(id));
studentService.delete(st);
response.getWriter()。print("{\"del\":\"true\"}");
}
catch(Exception e){
log.error(e.getMessage());
e.printStackTrace();
}
}
}
service类实现
package com.mvc.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.mvc.dao.EntityDao;
import com.mvc.entity.Student;
@Service
public class StudentService {
@Autowired
private EntityDao entityDao;
@Transactional
public ListgetStudentList(){
StringBuffer sff=new StringBuffer();
sff.append("select a from ")。append(Student.class.getSimpleName())。append(" a ");
Listlist=entityDao.createQuery(sff.toString());
return list;
}
public void save(Student st){
entityDao.save(st);
}
public void delete(Object obj){
entityDao.delete(obj);
}
}
OK,例子写完。有其它业务内容,只需直接新建view,并实现相应comtroller和service就行了,配置和dao层的内容基本不变,也就是每次只需写jsp(view),controller和service调用dao就行了。
怎样,看了这个,spring mvc是不是比ssh实现更方便灵活。
- controller类实现,只需把注解写上
- (亲测有效。)Spring MVC中,Controller中使用service只需使用注解,但是普通类获取 service或 dao 都是null空的。
- @Controller注解
- @Controller注解
- 3. Component、Controller、Service、Repository注解类
- Spring Aop自定义注解拦截Controller实现日志管理
- Spring自定义注解实现Controller获取想要的数据
- ci控制器controller只支持1级目录,如需多级要扩展路由类
- MVC只Controller跳转
- spring MVC Controller 实现接口后,不能解析@Controller注解问题
- 以后把想到的都写上来。随时翻翻
- 基于注解的Controller
- Spring控制器注解Controller
- springMVC controller注解整理
- Spring注解@Controller 详解
- SpringMVC Controller注解
- Spring注解@Controller 详解
- Springmvc -> Controller注解
- andorid 调用自己写的WebService
- ArrayList源码阅读笔记
- 弃用了struts,用spring mvc框架做了几个项目
- 在Linux下卸载Oracle 11g
- fedora-vim-conf-lite
- controller类实现,只需把注解写上
- 利用rman自动备份转储spfile
- JSP的九大隐式对象之------PageContext
- C++ 4种类型转换详解
- Eclipse下Bytecode Outline插件安装
- 生产环境提升rman备份速度----启动块跟踪
- hibernate.properties数据库连接配置
- 有 1000 个一模一样的瓶子,其中有 999 瓶是普通的水,有一瓶是毒药。任何喝下毒药的生物都会在一星期之后死亡。现在,你只有 10 只小白鼠和一星期的时间,如何检验出哪个瓶子里有毒药?
- ubuntu12.0.4apache安装mod_security模块