Java 我能';t使用SpringMVC3访问jsp页面

Java 我能';t使用SpringMVC3访问jsp页面,java,spring,spring-mvc,Java,Spring,Spring Mvc,我创建了名为addContact.jsp的简单添加实体表单 <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <body> <h2>Contact information</h2> <form:form method="POST" action="contacts/add" modelAttribute=

我创建了名为addContact.jsp的简单添加实体表单

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>

<body>
    <h2>Contact information</h2>
    <form:form method="POST" action="contacts/add" modelAttribute="contact">
        <table>
            <tr>
                <td><form:label path="name">Name</form:label></td>
                <td><form:input path="name" /></td>
            </tr>
            <tr>
                <td><form:label path="surname">Surname</form:label></td>
                <td><form:input path="surname" /></td>
            </tr>
            <tr>
                <td><form:label path="phonenumber">Phonenumber</form:label></td>
                <td><form:input path="phonenumber" /></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="Submit" /></td>
            </tr>
        </table>
    </form:form>
</body>

</html>

联系方式
名称
姓
电话号码
下面是rest-handler-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="facade.rest" />

    <mvc:annotation-driven />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

下面是web.xml中的servlet映射

<servlet-mapping>
    <servlet-name>rest-handler</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

休息处理器
/
最后是控制器:

package facade.rest;

import java.util.List;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import domain.ContactDO;
import service.IContactService;


@Controller
public class ContactController {

  private IContactService contactService;


  @Required
  public void setContactService(IContactService contactService) {
    this.contactService = contactService;
  }


  @RequestMapping(value = "/contacts", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  public @ResponseBody ResponseEntity<List<ContactDO>> getContacts() {
    List<ContactDO> contacts = contactService.load();
    if(contacts != null) {
      return new ResponseEntity<>(contacts, HttpStatus.OK);
    }
    else
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
  }


  @RequestMapping(value = "/contacts/{id:.*}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  public @ResponseBody ResponseEntity<ContactDO> getContact(@PathVariable Integer id) {
    ContactDO contact = contactService.loadContact(id);
    if(contact != null) {
      return new ResponseEntity<>(contact, HttpStatus.OK);
    }
    else
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
  }


  @RequestMapping(value = "/contacts/add", method = RequestMethod.POST)
  public String addContact(@ModelAttribute("contact") ContactDO contact, ModelMap model) {
    model.addAttribute("name", contact.getName());
    model.addAttribute("surname", contact.getSurname());
    model.addAttribute("phonenumber", contact.getPhonenumber());
    //contact.setId(2);
    contactService.store(contact);
    return "contacts";
  }

}
package facade.rest;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Required;
导入org.springframework.http.HttpStatus;
导入org.springframework.http.MediaType;
导入org.springframework.http.ResponseEntity;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.ModelMap;
导入org.springframework.web.bind.annotation.ModelAttribute;
导入org.springframework.web.bind.annotation.PathVariable;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.ResponseBody;
导入domain.ContactDO;
导入服务.IContactService;
@控制器
公共类联系人控制器{
私人IContactService联系人服务;
@必需的
公共无效设置contactService(IContactService contactService){
this.contactService=contactService;
}
@RequestMapping(value=“/contacts”,method=RequestMethod.GET,products=MediaType.APPLICATION\u JSON\u value)
public@ResponseBody ResponseEntity getContacts(){
List contacts=contactService.load();
如果(联系人!=null){
返回新的响应状态(联系人,HttpStatus.OK);
}
其他的
返回新的ResponseEntity(未找到HttpStatus.NOT_);
}
@RequestMapping(value=“/contacts/{id:.*}”,method=RequestMethod.GET,products=MediaType.APPLICATION\u JSON\u value)
public@ResponseBody ResponseEntity getContact(@PathVariable整数id){
ContactDO contact=contactService.loadContact(id);
如果(联系人!=null){
返回新的响应状态(联系人,HttpStatus.OK);
}
其他的
返回新的ResponseEntity(未找到HttpStatus.NOT_);
}
@RequestMapping(value=“/contacts/add”,method=RequestMethod.POST)
公共字符串addContact(@modeldattribute(“contact”)ContactDO contact,ModelMap model){
model.addAttribute(“name”,contact.getName());
model.addAttribute(“姓氏”,contact.getNames());
model.addAttribute(“phonenumber”,contact.getPhonenumber());
//联系人:setId(2);
contactService.store(联系人);
返回“联系人”;
}
}
当我试图从at(pb是war的名字)访问时,我得到了HTTP 400。日志告诉我正在尝试执行get请求

当我尝试从处访问时 (pb是战争的名字)我得到HTTP 400日志告诉我正在尝试执行get请求

没错!!您正在使用GET请求尝试
/contacts/add
,请求处理程序无法映射您的请求,因为只有通过POST请求才能访问
/contacts/add
。查看您的方法定义:

@RequestMapping(value = "/contacts/add", method = RequestMethod.POST)
  public String addContact(@ModelAttribute("contact") ContactDO contact, ModelMap model) {
}
您需要一个POST请求才能访问您的方法

编辑:我想您想用
/contacts/add
方法编辑联系人。如果是这种情况,请先拨打
/contacts/{id:.*}
,将
id
作为您的联系人id。然后你可以在那里进行编辑,并通过提交编辑来发送帖子。这就是您联系您的方式
/contacts/add
页面

如果您不知道任何
id
,请先打电话 为您的联系人。在那里你会(希望)找到你的联系人进行编辑


编辑**:在您的rest客户端上,您是否尝试过将请求类型更改为
POST

那么我应该如何访问此页面?我应该从某处使用重定向吗?我可以使用这些get请求获取JSON。我尝试了Firefox的RESTClient扩展。我可以发送post请求,但jsp页面未打开1-您需要哪个联系人id?2-您正在使用哪个Rest客户端?1。我不需要id,我只想添加新的联系人实体。2.只有联系人/添加不起作用我想我明白我做错了什么。我使用POST请求访问jsp页面。我应该用它去那里。提交时,我应该在表单中使用POSTJust change action=“contacts/add”to action=“/contacts/add”,然后立即尝试。不起作用