英文:
How can i use the value which i got from a form as an variable in my dao class?
问题
我正在尝试创建一个页面,在该页面上我将输入类似于Java的兴趣,然后将从数据库中获取所有兴趣为Java的行,因此我已经创建了这个表单元素:
<input class="form-control" type="text" placeholder="Search..." name="interest"><br>
这是我控制器中的映射:
@ModelAttribute("courses")
public Map<String, String> getCourseList() throws SQLException, IOException {
return (new ServiceImpl()).getCourseList();
}
@RequestMapping(value="showCourses", method=RequestMethod.POST)
public ModelAndView showCourses(@RequestParam("interest") String interest) {
ModelAndView mv = new ModelAndView();
mv.addObject("interest", interest);
mv.setViewName("showCourses");
return mv;
}
我正在从数据库中获取所有课程,但在我的daoimpl类中:
public Map<String, String> courseList() throws SQLException, IOException {
connect();
Map<String, String> courseList = new HashMap<String, String>();
String sql = "SELECT * from course;";
resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
courseList.put(resultSet.getString("CourseName"), resultSet.getString("CourseName"));
}
return courseList;
}
在courseList方法中,我想使用兴趣,以便我只能获取特定兴趣的课程,并在我的jsp上显示它们,我如何在daoImpl类中使用兴趣?
英文:
I am trying to create a page on which i will type the interest like java and then i will fetch all the rows from database which has interest as java so i have created this form element
<input class="form-control" type="text" placeholder="Search..." name="interest"><br>
This is my mapping in my controller
@ModelAttribute("courses")
public Map<String, String> getCourseList() throws SQLException, IOException {
return (new ServiceImpl()).getCourseList();
}
@RequestMapping(value="showCourses", method=RequestMethod.POST)
public ModelAndView showCourses(@RequestParam("interest") String interest) {
ModelAndView mv=new ModelAndView();
mv.addObject("interest",interest);
mv.setViewName("showCourses");
return mv;
}
I am fetching all the courses from the database but in my daoimpl class
public Map<String, String> courseList() throws SQLException, IOException {
connect();
Map<String, String> courseList = new HashMap<String, String>();
String sql = "SELECT * from course;";
resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
courseList.put(resultSet.getString("CourseName"), resultSet.getString("CourseName"));
}
return courseList;
}
in this courseList method i want to use the interest so that i can only fetch the courses which are of specific interest and show them on my jsp how can i use the interest in my daoImpl class?
答案1
得分: 1
请查看下面的代码片段。
您已经实现了DAO,只需要对该方法进行少量修改:-
public Map<String, String> getCourseList(String subjectInterest) {
connect();
Map<String, String> courseList = new HashMap<String, String>();
String sql = "SELECT * from course where subInterest = ?";
statement.setString(1,subjectInterest);
resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
courseList.put(resultSet.getString("CourseName"), resultSet.getString("CourseName"));
}
return courseList;
}
除此之外,您还需要实现一个服务类,从该类中可以调用DAO方法来获取课程详情,如下所示:-
@Service
public class CourseService {
@Autowired
private CourseDAO courseDao;
public Map<String, String> getCourseList(String subjectInterest) throws SQLException, IOException {
return courseDao.getCourseList(subjectInterest);
}
}
在控制器中稍微进行一些更改,如下所示:-
@Controller
public class CourseController {
@Autowired
private CourseService courseService;
@RequestMapping(value = "/showCourses", method = RequestMethod.POST)
public String showCourses(@RequestParam("interest") String interest, Model model) throws SQLException, IOException {
model.addAttribute("interest", interest);
Map<String, String> courseList = courseService.getCourseList(interest);
model.addAttribute("courseList", courseList);
return "showCourses";
}
}
最后,您需要在您的 showCourses.jsp 中循环遍历 courseList 属性,这样您就可以根据兴趣展示课程。
英文:
Please have a look in below code snippet.
you have already implemented dao just need little modification on that method :-
public Map<String, String> getCourseList(String subjectInterest) {
connect();
Map<String, String> courseList = new HashMap<String, String>();
String sql = "SELECT * from course where subInterest = ?";
statement.setString(1,subjectInterest);
resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
courseList.put(resultSet.getString("CourseName"), resultSet.getString("CourseName"));
}
return courseList;
}
Along with this you need to implement one service class from where you can call your dao method for fetching the course details like below :-
@Service
public class CourseService {
@Autowired
private CourseDAO courseDao;
public Map<String, String> getCourseList(String subjectInterest) throws SQLException, IOException {
return courseDao.getCourseList(subjectInterest);
}
}
Post this in your controller need to change little bit something like below :-
@Controller
public class CourseController {
@Autowired
private CourseService courseService;
@RequestMapping(value = "/showCourses", method = RequestMethod.POST)
public String showCourses(@RequestParam("interest") String interest, Model model) throws SQLException, IOException {
model.addAttribute("interest", interest);
Map<String, String> courseList = courseService.getCourseList(interest);
model.addAttribute("courseList", courseList);
return "showCourses";
}
}
And finally you need to loop courseList attribute in your showCourses.jsp, So you will be able to show courses based on interest.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论