基于注解的spring mvc controller

来源:互联网 发布:网络直播举报 编辑:程序博客网 时间:2024/06/06 11:05

1、配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--设置DispatcherSerlet的配置文件位置-->
<init-param>
<param-name>contextConfigLocation</param-name>
<!--所有基于src下的文件最后部署都会到/WEB-INF/classes/下-->
<param-value>/WEB-INF/classes/spring/Servlet/*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

2、根据web.xml上的路径配置,servlert.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!--设置对应的扫描包-->
<context:component-scan base-package="test.controller"/>
<!--启用Spring mvc注解功能-->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!--定义视图解析器的bean-->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

3、定义路径变量Controller

@Controller
@RequestMapping("/test")
//resetful风格
public class startController {
@RequestMapping(value = "/start/{name}/{age}",method = RequestMethod.GET)
public String start_get(@PathVariable("name") String name, @PathVariable("age") int nianling){

System.out.println(name);
System.out.println(nianling);
return "start_get";

}
@RequestMapping(value = "/start/{date}",method = RequestMethod.GET)
public String start(@PathVariable("date") Date date){
System.out.println(date.toString());

return "start_get";
}
@InitBinder
public void initBinder(WebDataBinder binder){
binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyyMMdd"),false));

}

/*
@RequestMapping(value = "/start/{name}/{age}",method = RequestMethod.POST)
public String start_post(@PathVariable("name") String name,@PathVariable("age") int nianling){

System.out.println(name);
System.out.println(nianling);
return "start_post";

}
*/
/*@RequestMapping(value = "/start",method = RequestMethod.GET)
public String start_get(){


return "start_get";

}
@RequestMapping(value = "/start",method = RequestMethod.POST)
public String start_post(){


return "start_post";

}*/


}

4、Controller方法的可以传入的参数

public void testAllrequments(HttpServletRequest request, HttpServletResponse response, HttpSession session, @PathVariable anyType name, @RequestParam anyType parames, @CookieValue AnyType cookieName,@RequestHeader("content-type") anytype header)

5、@Controller方法中的参数一些使用技巧

@RequestMapping("/test/{name}")
public String testAllreturnrequments(PrintWriter out, HttpServletResponse response, Map model){

// out = response.getWriter();
//out.print(); 直接往客户端去写
model.put("","");//可以在参数中直接定义一个model,方法里面直接输入值就可以.方法自动帮我们初始化.
return "viewName";


}

6、command对象绑定

@RequestMapping("/test/user")
//当请求:127.0.0.1/test/test/user.do?name=zhangsan&password=1223231当中的name和password会自动绑定到User对象中
public String testCommand(User user, BindingResult result){//BindingResult command对象绑定过程中如果发生错误,就会在result中

return "";
}

7、@controller中返回值类型

@RequestMapping("/test/requestReturn")
public void|String|User|Model|ModelAndView testReturnParams(){
//当返回的是void时候,实际上生成隐含的viewName--->按照请求路径${applicacotionContext}/test/test/requestReturn.do---->会找/WEB-INFO/page/test/test/requestReturn.jsp文件
//当只返回字符串的时候为"ViewName"当方法里面有model,他会自动的返回model
//可以返回任意类型对象,例子,比如返回User对象,会自动填充到model当中,model的key为"类名" value肯定是对象拉.model.put("user",User).视图就会按默认解析方式解析出来一个视图名称
//如果返回list<User>对象 ,model中的key名字为UserList
return "";

}







0 0