私は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クラスで関心をどのように使用できますか?
以下のコードスニペットをご覧ください。
あなたはすでに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メソッドを呼び出すことができるサービスクラスを1つ実装する必要があります。
@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でコースリスト属性をループする必要があるため、興味に基づいてコースを表示できます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加