2013-04-29 903 views
0

我想Spring MVC的例子,我不能够控制从JSP传送到控制器class.My控制器类是如下Spring MVC的请求的资源不可用

CController.java

package project4; 

import project4.UserDAO1; 
import project4.User1; 
import java.util.Map; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.springframework.stereotype.Controller; 
import org.springframework.transaction.annotation.Transactional; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.servlet.mvc.multiaction.MultiActionController; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.validation.BindingResult; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Qualifier; 


@Controller 
@RequestMapping("/frm4") 


public class CController{ 

    private UserDAO1 userDAO; 
    @Autowired 
    @Qualifier("myUserDAO") 
    private UserDAOImpl1 myUserDAO; 

    public void setUserDAO(UserDAO1 userDAO) { 
     this.userDAO = userDAO; 
    } 

    @RequestMapping(params = "add", method = RequestMethod.POST) 
    public ModelAndView add(@ModelAttribute("add") User1 user,HttpServletRequest 
      request,HttpServletResponse response) throws Exception { 
     userDAO.saveUser(user); 
     System.out.println("hai"); 
     return new ModelAndView("redirect:list.htm"); 
    } 

    @RequestMapping(params = "delete", method = RequestMethod.POST) 
    @Transactional 
    public ModelAndView delete(@ModelAttribute("delete") User1 user,HttpServletRequest 
      request,HttpServletResponse response) throws Exception { 
      userDAO.deleteUser(user); 
      return new ModelAndView("redirect:list.htm"); 
    } 


    @RequestMapping(params = "find", method = RequestMethod.POST) 
    @Transactional 
    public ModelAndView find(@ModelAttribute("find") User1 user,HttpServletRequest 
      request,HttpServletResponse response) throws Exception { 
        userDAO.findUser(user); 
         return new ModelAndView("redirect:list.htm"); 
    } 


    @RequestMapping(params = "update", method = RequestMethod.POST) 
    @Transactional 
    public ModelAndView update(@ModelAttribute("update") User1 user,HttpServletRequest 
      request,HttpServletResponse response) throws Exception { 
        userDAO.updateUser(user); 
         return new ModelAndView("redirect:list.htm"); 
    } 

    public ModelAndView list(HttpServletRequest request, 
     HttpServletResponse response) throws Exception { 


     ModelMap modelMap = new ModelMap(); 
     modelMap.addAttribute("userList", userDAO.listUser()); 
     modelMap.addAttribute("user", new User1()); 
     return new ModelAndView("userForm", modelMap); 
    } 
} 

和JSP页面如下

frm4.jsp

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<form method="POST" action="add.do"> 
    <table> 
    <tr> 
     <td><label for="id">Id:</label></td> 
     <td> 
      <input name="id" value="${user.id}" /> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      <label for="name">Name:</label></td> 
     <td> 
      <input name="name" value="${user.name}" /> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      <label for="password">Password:</label></td> 
     <td> 
      <input name="password" value="${user.password}" /> 
     </td> 
     </tr> 

     <tr> 
      <td> 
       <label for="gender">Gender:</label></td> 
      <td> 
       <input name="gender" value="${user.gender}" /> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <label for="lastName">Country:</label></td> 
      <td> 
      <input name="country" value="${user.country}" /> 
      </td> 
     </tr> 

    </table> 
    <input type="submit" value="Submit"/> 
    </form> 
    </body> 
</html> 

我我不使用jsp中春表单标签(因为它扔我“既不结合也不是普通的目标对象”错误)。所以,当我运行这个程序,我收到以下错误作为

type Status report 

message 

description The requested resource() is not available. 

和我得到的网址

http://localhost:8080/Spring/add.do 

其中春季是下我已经存储了program.I都划归在src.I“project4”文件夹中的CController.java控制器类我的项目名称已经把frm4.jsp和的List.jsp在WebContent下。

编辑:

我试图

<form method="POST" action="CController/add.do"> 

<form method="POST" action="Spring/CController/add.do"> 

但错误是一样的

+0

'行动= “FRM4/add.do”'? – NINCOMPOOP 2013-04-29 05:33:54

+0

我也试过,但错误仍然是一样的 – Ezhil 2013-04-29 05:36:04

回答

1

您还没有定义/add

的任何请求映射

你只需要/frm4你如同附加分离由PARAM名称的行为,删除等

对于这个工作使用action="/frm4"并在表单中添加一个隐藏PARAM名add

但我会建议将其更改为独立网址FRM4 /添加,FRM4 /删除等

@RequestMapping(value = "/add", method = RequestMethod.POST) 
+0

我将它改为@RequestMapping(value =“frm4/add”,method = RequestMethod.POST)但错误保持不变 – Ezhil 2013-04-29 06:30:23

+0

注意值应该是/ add而不是frm4 /添加作为您的控制器在顶部已经有/ frm4。 – 2013-04-29 06:58:29

0

在您的spring配置中使用<context:component-scan base-package="<YOUR PACKAGE HERE>"/>为spring扫描注释(如果尚未完成)。

类似的问题:Spring MVC Component Scan

+0

我已经在配置文件中提供了组件扫描... – Ezhil 2013-04-29 06:21:18

1

的人,你必须设定行动在形式上与 “/ FRM4”

,改变

<input type="submit" value="Submit"/> 

<input type="submit" name="add" value="Submit"/> 

@ModelAttribute("add") User1 user 

@ModelAttribute User1 user 

请这些videos