标注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>
精慕HU
相关分类