jsp post总是在控制器中调用意外的方法

时间:2014-10-08 14:56:58

标签: java spring jsp spring-mvc

我是Spring的新手。

我正在尝试学习Spring并使用此处提供的示例 http://elizabetht.github.io/blog/2013/11/21/student-enrollment-using-mybatis-with-spring/

这有一个登录页面,最后它登录用户并委托成功页面,现在我用数据表改变了成功页面,从数据库中读取。 需要的是,一旦用户点击提交,数据必须保存到数据库,并且必须更新当前表。

所以我编辑了成功页面 -

 <div id="containers">
<%
StudentLogin name = (StudentLogin)request.getAttribute("name");
List<Cube> personList = (List)request.getAttribute("message");
String userName = name.getUserName();
for(Cube cube: personList){
    String usedBy =  cube.getUsedby().trim();
    boolean alreadyUsed = (usedBy.length()>0);
    String ipAddress = cube.getIp();
    String cubeName = cube.getCubename();
    String details = cube.getDetails();
    String mainClass = (alreadyUsed)?"pricetab":"pricetabmid";
    String buttonName = (alreadyUsed)?"Blocked":"Click Here!";
    String usedBody = (alreadyUsed)?usedBy:userName;
    String buttonclass = (alreadyUsed)?"button":"buttonmid";
    String priceFooterClass =(alreadyUsed)?"pricefooter":"pricefootermid";
%>
    <div class="<%= mainClass%> ">
    <h1> <%= ipAddress%> </h1>
    <div class="price"> 
        <h2> <%= cubeName%> </h2> 
     </div>
         <div class="infos">  
         <h3>  <%= details%> </h3>
         <h2>  <%= usedBody%> </h2>
     </div>
      <div class="<%= priceFooterClass%> ">
        <div class="<%= buttonclass%> ">
          <a href="#" onClick="suc(name)"> <%= buttonName%> </a>
        </div>
      </div>
    </div>
<% } %>
</div>

现在你可以注意到最终的onClick,所以我提供了这个应该进行post调用的脚本,并且必须委托给控制器中的方法 -

 <script>
        function suc(name){
            var username = "";
            var password = "";
            var studentLogin = name;
            $.post('success', { userName : userName,password:password }, function(data) {
                $('#containers').html(data).hide().slideDown('slow');
            } );
        }

</script>

我的控制器是 -

@Controller
@Scope("session")
public class StudentController {

    private static final Logger logger = Logger.getLogger(StudentController.class);

    @Autowired
    private StudentService studentService;

    @Autowired
    private CubeService cubeService;

    @RequestMapping(value="/signup", method=RequestMethod.GET)
    public String signup(Model model) {
        Student student = new Student();        
        model.addAttribute("student", student);     
        return "signup";
    }

    @RequestMapping(value="/signup", method=RequestMethod.POST)
    public String signup(@Valid @ModelAttribute("student") Student student, BindingResult result, Model model) {        
        if(result.hasErrors()) {
            return "signup";
        } else if(studentService.findByUserName(student.getUserName())) {
            model.addAttribute("message", "User Name exists. Try another user name");
            return "signup";
        } else {
            studentService.save(student);
            model.addAttribute("message", "Saved student details");
            return "redirect:login.html";
        }
    }

    @RequestMapping(value="/login", method=RequestMethod.GET)
    public String login(Model model) {          
        StudentLogin studentLogin = new StudentLogin();     
        model.addAttribute("studentLogin", studentLogin);
        return "login";
    }


    @RequestMapping(value="success", method=RequestMethod.GET)
    public String success(Model model) {            
        CubeData cubeData = new CubeData();     
        model.addAttribute("cubeData", cubeData);
        return "success";
    }

    @RequestMapping(value="success", method=RequestMethod.POST)
    public String success(@Valid @ModelAttribute("cubeData") CubeData cubeData, BindingResult result,Model model) {     
        logger.debugf("sss", result);
        return "signup";
    }



    @RequestMapping(value="/login", method=RequestMethod.POST)
    public String login(@Valid @ModelAttribute("studentLogin") StudentLogin studentLogin, BindingResult result,Model model) {
        logger.debug(result);
        logger.debug(studentLogin);
        if (result.hasErrors()) {
            return "failure";
        } else {
            boolean found = studentService.findByLogin(studentLogin.getUserName(), studentLogin.getPassword());
            if (found) {        
                List<Cube> cubes = cubeService.getCubes();
                model.addAttribute("name", studentLogin);
                model.addAttribute("message", cubes);
                return "success";
            } else {                
                return "failure";
            }
        }

    }
}

我尝试运行许多烫发组合,但总是点击它再次委托我登录页面的按钮并抛出错误userName未找到,但我想知道为什么它不会去定义的成功块控制器。

1 个答案:

答案 0 :(得分:0)

问题在于post方法的url ...改变了它并且它起作用了

$.post('/test/success.html', { userName : userName,password:password }, function(data) {
                $('#containers').html(data).hide().slideDown('slow');
            } );