为了账号安全,请及时绑定邮箱和手机立即绑定

spring mvc控制器@RequestMapping注解不能使用

spring mvc控制器@RequestMapping注解不能使用

LEATH 2019-02-22 12:30:32
标注RequestMapping后无法通过 http://localhost:8082/Test/FormAdd 地址访问到 提示404错误 只能通过 http://localhost:8082/FormAdd,问下怎么配置 代码如下 : package com.springapp.mvc; import com.data.dataaccess.HibernateHelper; import com.data.model.Student; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import javax.management.Attribute; @Controller @RequestMapping(name="/Test") public class TestController { @RequestMapping(value = "/TestAttr",method = {RequestMethod.GET,RequestMethod.HEAD}) public String TestAttr(ModelMap model) { Class<MyClass> item = MyClass.class; if(item.isAnnotationPresent(MyAttribute.class)){ model.addAttribute("message", "has annotation myattribute"); MyAttribute itemAttr =(MyAttribute) item.getAnnotation(MyAttribute.class); model.addAttribute("value", itemAttr.value()); } return "/test/testAttr"; } @RequestMapping(value = "/FormAdd",method = {RequestMethod.GET}) public String TestFormAdd(ModelMap model) { return "/test/addStudent"; } @RequestMapping(value = "/FormAdd",method = {RequestMethod.POST}) public String TestFormAdd(ModelMap model,Student student) { try { org.hibernate.Session session = HibernateHelper.GetFactory().openSession(); session.beginTransaction(); session.save(student); session.getTransaction().commit(); model.addAttribute("statu","success"); } catch (Exception e){ model.addAttribute("statu","failed"+e.getMessage()); } return "/test/addStudent"; } } xml配置: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.springapp.mvc"/> <!-- 静态资源(js、image等)的访问 --> <mvc:default-servlet-handler/> <!-- 开启注解 --> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
查看完整描述

1 回答

?
精慕HU

TA贡献1845条经验 获得超8个赞

@RequestMapping(name="/Test")

@RequestMapping(value = "/Test")
@RequestMapping("/Test")
查看完整回答
反对 回复 2019-03-01
  • 1 回答
  • 0 关注
  • 2425 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信