Java 中 Controller 层基础代码
程序员文章站
2022-03-16 14:32:50
...
Java 中 Controller 层基础代码
此文件会随着时间不断更新
package com.hyd.YKenan.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.YKenan.YKenan.pojo.YHide;
import com.YKenan.YKenan.service.YHideService;
@Controller
public class YHideController {
@Autowired
private YHideService yHideService;
@RequestMapping("/list")
public String list(Model model) {
try {
List<YHide> all = yHideService.getAll();
model.addAttribute("yHideList", all);
} catch (Exception e) {
e.printStackTrace();
}
return "list";
}
@RequestMapping(value = "/list/{yId}")
public String delect(@PathVariable(value = "yId") String yId) {
try {
yHideService.deleteByPrimaryKey(yId);
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:/list";
}
@RequestMapping(value = "/update/{yId}", method = RequestMethod.GET)
public String update(@PathVariable(value = "yId") String yId, HttpServletRequest request, Model model) {
try {
YHide selectByPrimaryKey = yHideService.selectByPrimaryKey(yId);
String getyAge = selectByPrimaryKey.getyAge();
String getyHoppy = selectByPrimaryKey.getyHoppy();
request.getSession().setAttribute("update_selectByPrimaryKey", selectByPrimaryKey);
model.addAttribute("getyAge", getyAge);
model.addAttribute("getyHoppy", getyHoppy);
} catch (Exception e) {
e.printStackTrace();
}
return "update";
}
@RequestMapping(value = "update/add/", method = RequestMethod.POST)
public String add(@RequestParam String age, @RequestParam String hoppy, HttpServletRequest request, Model model) {
try {
if ("".equals(age) || age.isEmpty() || "".equals(hoppy) || hoppy.isEmpty()) {
} else {
YHide attribute = (YHide) request.getSession().getAttribute("update_selectByPrimaryKey");
attribute.setyAge(age);
attribute.setyHoppy(hoppy);
yHideService.update(attribute);
model.addAttribute("attribute" + "用户修改成功", attribute.getyId());
}
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:/list";
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String yHideAdd(@RequestParam String age, @RequestParam String hoppy, Model model) {
try {
if ("".equals(age) || age.isEmpty() || "".equals(hoppy) || hoppy.isEmpty()) {
} else {
String insertAndReturnID = yHideService.insertAndReturnID(age, hoppy);
if (insertAndReturnID.equals("") || insertAndReturnID.isEmpty()) {
model.addAttribute("用户添加失败", insertAndReturnID);
}
model.addAttribute("insertAndReturnID" + "用户添加成功", insertAndReturnID);
}
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:/list";
}
@RequestMapping("/delect")
public String yHideDelect(@RequestParam String id, Model model) {
try {
if ("".equals(id) || id.isEmpty()) {
} else {
yHideService.deleteByPrimaryKey(id);
}
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:/list";
}
}
上一篇: 如何获取view的controller
下一篇: Android基础之动画资源浅析
推荐阅读
-
Java基础之内部类的实例代码详解
-
JavaEE基础day02 1.定义Java中的变量 四类八种 2.变量定义和使用的注意事项 3.数据类型的转换、强制数据类型转换4.算数运算符、比较运算符、逻辑运算符、赋值运算符、三元运算符
-
mysql-请问:用java代码把不同的excel形式加载到MySQL数据库中(循环判断空就停止)?谢谢!
-
Android TextView 在java代码中改变字体的颜色的方法
-
javascript中字符串的定义示例代码_基础知识
-
Javascript中的转义用法实例代码_基础知识
-
在HTML中插入JavaScript代码的示例_基础知识
-
idea创建一个入门Spring Boot项目(controller层)使用Moven代码管理
-
MongoDB之Java测试代码(DAO层)
-
mysql-请问:用java代码把不同的excel形式加载到MySQL数据库中(循环判断空就停止)?谢谢!