`
cloudtech
  • 浏览: 4614028 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

spring3 web mvc framework

 
阅读更多

Spring MVC是一个构建于Spring Framework之上的现代Web应用程序框架. Spring3 全面支持REST风格的Web服务,"We're really seeing extensive interest and growth in REST, and it will have comprehensive support for RESTful Web services," said Johnson.spring3再加上对annotation的支持如虎添翼!

web.xml配置

<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:*controller.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

web.xml中定义了一个名为annomvc的SpringMVC模块,按照SpringMVC的契约,需要在WEB-INF/annomvc-servlet.xml配置文件中定义SpringMVC模块的具体配置 ,这里我们修改到*controller.xml中。

*controller.xml中加入

<!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.alan.manager.controller"/>

<!-- 使用tiles视图名解析器,即控制器返回的视图名均在tiles文件中定义 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/view/"/>
<property name="suffix" value=".jsp"></property>
</bean>

//注释功能开启

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

Java代码:

@Controller
@RequestMapping("/resource.do")

public class TestController extends BaseController {
@Autowired
private TestDao testDao;

//用于ajax中,返回void

@RequestMapping(params = "method=validatName",method = RequestMethod.GET)
public void validatName(HttpServletResponse response, @RequestParam(value = "enName", required = false)String name) throws IOException {
Query<testDefinition> query = testDao.createQuery();
query.filter("enName", name);
List list = testDao.find(query).asList();
if(list != null && list.size() > 0){
response.getWriter().write(String.valueOf(false));
}else{
response.getWriter().write(String.valueOf(true));
}
response.getWriter().flush();
response.getWriter().close();
}

@RequestMapping(params = "method=test",method = RequestMethod.POST)

public ModelAndView test2(HttpServletRequest arg0, ②响应请求的方法
HttpServletResponse arg1) throws Exception {
return new ModelAndView("index","greeting",greeting); ③返回一个ModelAndView对象
}

@RequestMapping(params = "method=test3")

public String test3(Model model) throws Exception {

model.addAttribute("resource", "hehh");

return "test3"

}

}

通过resource.do?method=validatName请求上面第一个方法。上面代码描述了controller中返回的几种形式,validatName返回text文本,不进行页面跳转,test2跳转到/view/index.jsp页面中,test3跳转到/view/test3.jsp,每个方法传入的参数可以是不同的,根据需要传入。

注意事项:

@Controller将该类注释为controller

1处理多个请求

在每个方法前面使用@RequestMapping("/名称.do")不同的名称处理不同的请求缺点:xxx.do太多不利于跟踪,复制程度增加

在controller类名前面定义@RequestMapping("/名称.do")在方法前添加注解

@RequestMapping(params="method=listAllBoard")则处理有此参数的请求多一个参数思路清楚

2处理不同的http请求:

在方法前面增加注释:RequestMethod有POST,GET如:@RequestMapping(params="method=test",method=RequestMethod.POST)

3参数处理:(默认这样处理,也是约定优于设计原则自动转换类型)自由选择

普通属性:如果入参是基本数据类型(如intlongfloat等),URL请求参数中一定要有对应的参数,否则将抛出TypeMismatchException异常,提示无法将null转换为基本数据类型。

对象及属性:Userbean的userId属性的类型是基本数据类型,但如果URL中不存在userId参数,Spring也不会报错,此时Userbean.userId值为0。如果Userbean对象拥有一个dept.deptId的级联属性,那么它将和dept.deptId参数绑定。

4参数处理:(明确指定参数匹配自动转换类型)

普通属性和对象及属性:@RequestParam("id")注解,所以它将和id的URL参数绑定

5绑定域中的某个属性:

Model ModelMap类,它作为通用的模型数据承载对象,传递数据供视图所用代替request.setParam

Session@SessionAttributes("currUser")//①将ModelMap中属性名为currUser的属性放到Session属性列表中,以便这个属性可以跨请求访问(类名前面写)

session多个参数!@SessionAttributes({“attr1”,”attr2”})。

取值绑定到方法上(@ModelAttribute("currUser")Useruser)

注解及解释:

@Autowired将分别寻找和它们类型匹配的Bean注入,@Autowired可以对成员变量、方法以及构造函数进行注释,ByType

如果有多个匹配结果或者没结果BeanCreationException异常

@Autowired(required=false),这等于告诉Spring:在找不到匹配Bean时也不报错。

@Qualifier注释指定注入Bean的名称,@Qualifier的标注对象是成员变量、方法入参、构造函数入参----------ByName

@Autowired@Qualifier结合使用时,自动注入的策略就从byType转变成byName

@Resource默认按byName自动注入

@InitBinder在controller方法前酌情使用,用于调用请求方法前的一些初始化

其他org.springframework.web.multipart.commons.CommonsMultipartResolver

org.springframework.web.servlet.view.json.MappingJacksonJsonView等根据需要在配置文件中配置

restful feature in spring mvc 3

spring mvc 3中rest风格的动态传参请求,java后台代码如下,一些值通过 URI传递。

@RequestMapping(value="/hotels/{hotel}/bookings/{booking}", method=RequestMethod.GET)
public String getBooking(@PathVariable("hotel") long hotelId, @PathVariable("booking") long bookingId, Model model) {
Hotel hotel = hotelService.getHotel(hotelId);
Booking booking = hotel.getBooking(bookingId);
model.addAttribute("booking", booking);
return "booking";
}

分享到:
评论

相关推荐

    spring-webmvc-5.0.8.RELEASE-API文档-中文版.zip

    标签:springframework、spring、webmvc、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...

    spring-webmvc-5.2.15.RELEASE-API文档-中文版.zip

    标签:springframework、spring、webmvc、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...

    spring-webmvc-5.3.10-API文档-中文版.zip

    标签:springframework、spring、webmvc、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...

    spring-webmvc-5.3.15-API文档-中文版.zip

    标签:spring、webmvc、springframework、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...

    spring-webmvc-5.0.8.RELEASE-API文档-中英对照版.zip

    标签:springframework、spring、webmvc、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明...

    spring-webmvc-5.3.7-API文档-中文版.zip

    标签:springframework、spring、webmvc、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...

    spring-webmvc-5.0.10.RELEASE-API文档-中文版.zip

    标签:spring、webmvc、springframework、jar包、java、API文档、中文版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和...

    spring-webmvc-5.3.7-API文档-中英对照版.zip

    标签:springframework、spring、webmvc、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明...

    spring-webmvc-5.1.3.RELEASE-API文档-中文版.zip

    标签:springframework、spring、webmvc、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...

    spring-webmvc-5.2.15.RELEASE-API文档-中英对照版.zip

    标签:springframework、spring、webmvc、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明...

    spring-webmvc5.3.6 jar包.rar

    这个jar文件包含Spring MVC框架相关...spriing-webmvc 依赖于 spring-web如果直接使用spring-webmvc,就会隐式地添加 spring-web。不必显示添加 spring-web。 该jar包含Spring MVC框架相关的所有类,如Servlets,Web MVC

    spring-webmvc-5.2.7.RELEASE-API文档-中文版.zip

    标签:springframework、spring、webmvc、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...

    Spring Framework最新版本 spring-webmvc-5.2.9.RELEASE

    避免可能会绕过CVE-2015-5211对RFD攻击的保护,具体取决于通过使用jsessionid路径参数使用的浏览器。

    spring-webmvc-5.3.15-API文档-中英对照版.zip

    标签:spring、webmvc、springframework、jar包、java、中英对照文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明...

    spring-webmvc-5.0.10.RELEASE-API文档-中英对照版.zip

    标签:spring、webmvc、springframework、jar包、java、API文档、中英对照版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释...

    spring-webmvc-5.2.7.RELEASE-API文档-中英对照版.zip

    标签:springframework、spring、webmvc、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明...

    Spring Web MVC framework中英文对照.pdf

    Spring Web MVC framework中英文对照,一份非常好的文档,翻译比较准确,易懂!

    spring-webmvc-5.2.0.RELEASE-API文档-中文版.zip

    标签:springframework、spring、webmvc、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...

    spring-webmvc-5.0.5.RELEASE-API文档-中文版.zip

    标签:springframework、spring、webmvc、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...

    spring-webmvc-4.2.2.RELEASE-API文档-中文版.zip

    标签:springframework、spring、webmvc、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...

Global site tag (gtag.js) - Google Analytics