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

查看:115
本文介绍了java - spring mvc控制器@RequestMapping注解不能使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

标注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>

解决方案

@RequestMapping(name="/Test")

@RequestMapping(value = "/Test")
@RequestMapping("/Test")

这篇关于java - spring mvc控制器@RequestMapping注解不能使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆