如何将从表单中获取的值作为变量在我的dao类中使用?

huangapple go评论122阅读模式
英文:

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

&lt;input class=&quot;form-control&quot; type=&quot;text&quot; placeholder=&quot;Search...&quot; name=&quot;interest&quot;&gt;&lt;br&gt;

This is my mapping in my controller

@ModelAttribute(&quot;courses&quot;)
	public Map&lt;String, String&gt; getCourseList() throws SQLException, IOException {

		return (new ServiceImpl()).getCourseList();
	}
	
	@RequestMapping(value=&quot;showCourses&quot;, method=RequestMethod.POST)
	public ModelAndView showCourses(@RequestParam(&quot;interest&quot;) String interest) {
		ModelAndView mv=new ModelAndView();
		mv.addObject(&quot;interest&quot;,interest);
		mv.setViewName(&quot;showCourses&quot;);
		return mv;
	}

I am fetching all the courses from the database but in my daoimpl class

public Map&lt;String, String&gt; courseList() throws SQLException, IOException {
		connect();
		Map&lt;String, String&gt; courseList = new HashMap&lt;String, String&gt;();
		String sql = &quot;SELECT * from course;&quot;;
		resultSet = statement.executeQuery(sql);
		while (resultSet.next()) {
			courseList.put(resultSet.getString(&quot;CourseName&quot;), resultSet.getString(&quot;CourseName&quot;));
		}
		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&lt;String, String&gt; getCourseList(String subjectInterest) {
	 connect();
     Map&lt;String, String&gt; courseList = new HashMap&lt;String, String&gt;();
     String sql = &quot;SELECT * from course where subInterest = ?&quot;;
     statement.setString(1,subjectInterest);
     resultSet = statement.executeQuery(sql);
     while (resultSet.next()) {
        courseList.put(resultSet.getString(&quot;CourseName&quot;), resultSet.getString(&quot;CourseName&quot;));
     }
     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&lt;String, String&gt; 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 = &quot;/showCourses&quot;, method = RequestMethod.POST)
    public String showCourses(@RequestParam(&quot;interest&quot;) String interest, Model model) throws SQLException, IOException {
	    model.addAttribute(&quot;interest&quot;, interest);
	    Map&lt;String, String&gt; courseList = courseService.getCourseList(interest);
	    model.addAttribute(&quot;courseList&quot;, courseList);
	    return &quot;showCourses&quot;;
    }

}

And finally you need to loop courseList attribute in your showCourses.jsp, So you will be able to show courses based on interest.

huangapple
  • 本文由 发表于 2020年9月3日 14:09:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63717806.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定