Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Spring MVC创建测验(Quastionare)+;JSP_Java_Spring_Jsp_Spring Mvc_Model View Controller - Fatal编程技术网

Java 使用Spring MVC创建测验(Quastionare)+;JSP

Java 使用Spring MVC创建测验(Quastionare)+;JSP,java,spring,jsp,spring-mvc,model-view-controller,Java,Spring,Jsp,Spring Mvc,Model View Controller,我需要你的帮助。没有你的帮助,我的大脑会爆炸!我正在SpringMVC+JSP上编写测验应用程序 我现在所做的: 1.我创建了HashMap+硬编码正确答案。 2.我创建了@RequestMapping(value=“/level_one”,method=RequestMethod.POST) 公共字符串levelOne(){ 返回“一级”; } 应该如何: 然后我就僵住了,我不知道该怎么做,也不知道如何将@RequestMaping中的代码与HashMap结合起来,并用jsp编写代码以使其可

我需要你的帮助。没有你的帮助,我的大脑会爆炸!我正在SpringMVC+JSP上编写测验应用程序

我现在所做的: 1.我创建了HashMap+硬编码正确答案。 2.我创建了
@RequestMapping(value=“/level_one”,method=RequestMethod.POST)
公共字符串levelOne(){
返回“一级”;
}

应该如何:

然后我就僵住了,我不知道该怎么做,也不知道如何将@RequestMaping中的代码与HashMap结合起来,并用jsp编写代码以使其可见和可点击??? 在《控制器》中,我写道:

 package ua.kiev.prog;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/")
public class MyController {

       final private String rightAnswerOne = "Dance";

    @ModelAttribute("answerList")
    public Map answerList() {
        Map<String, String> answerList = new HashMap<String, String>();
        answerList.put("one", "Sandbox");
        answerList.put("two", "Pixel");
        answerList.put("three", "Game");
        answerList.put("four", "Picture");

        return answerList;
    }


    @RequestMapping(value = "/level_one", method = RequestMethod.POST)
    public String levelOne() {
        return "levelone";
    }


}
包ua.kiev.prog;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.ModelAttribute;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入java.util.HashMap;
导入java.util.Map;
@控制器
@请求映射(“/”)
公共类MyController{
最终私人字符串rightAnswerOne=“舞蹈”;
@ModelAttribute(“应答列表”)
公共地图应答列表(){
Map answerList=new HashMap();
答案列表。放置(“一”、“沙箱”);
答案列表。放置(“两个”,“像素”);
答案列表。放置(“三”,“游戏”);
答案列表。放(“四”,“图片”);
返回应答列表;
}
@RequestMapping(value=“/level_one”,method=RequestMethod.POST)
公共字符串levelOne(){
返回“一级”;
}
}

@modeldattribute在请求映射之前执行。填充模型对象(未作为@requestmapping注释的方法上的参数传递)

您可以使用
model.asMap().Get(“yourkey”)访问该对象。

model.asMap().get(“应答列表”); ... 返回“levelone”;}

这就是modelattribute的工作原理,但这是一个很长的参数。您可以搜索一些关于SpringMVC的教程。 我向谷歌推荐mkyong spring mvc,他有一套庞大的spring mvc基础教程,易于理解

检查此链接
谢谢。我会这样做。但是在这个例子中,请回答另外一个问题。如果我像你以前写的那样编写代码。我应该如何编写jsp页面,来统一控制器和web视图中哈希映射的答案?因此,如果我单击web上的按钮,它将与服务器端页面上的哈希映射相统一。 那么我就可以写这样的东西了:

 public static int getCount(HashMap<String, String> answerList, String rightAnswerOne, String rightAnswerTwo, String message) {
    int count = 0;

    for (String tmp : answerList.values()) {
        if (rightAnswerOne.equals(tmp) ) {
            count++;
            System.out.println("SUCCESS");
        } else {
            System.out.println("DENIED");
        }
    }
    return count;
}
publicstaticintgetcount(HashMap应答列表、stringrighanswerone、stringrighanswertwo、stringmessage){
整数计数=0;
for(字符串tmp:answerList.values()){
如果(右应答一等于(tmp)){
计数++;
System.out.println(“成功”);
}否则{
System.out.println(“拒绝”);
}
}
返回计数;
}
我只是想了解如何写这些东西

 public static int getCount(HashMap<String, String> answerList, String rightAnswerOne, String rightAnswerTwo, String message) {
    int count = 0;

    for (String tmp : answerList.values()) {
        if (rightAnswerOne.equals(tmp) ) {
            count++;
            System.out.println("SUCCESS");
        } else {
            System.out.println("DENIED");
        }
    }
    return count;
}