spring mvc的学习

Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。


参考的文章:

http://elf8848.iteye.com/blog/875830/


http://blog.csdn.net/chichengit/article/details/12098111


OPEN文档




记录一些基本的使用方法【版本要比较新】

package com.smvc.action;

import java.io.File;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.smvc.modal.User;
import com.smvc.service.UserService;


@Controller
@RequestMapping("/test")
public class TestController {
	
	private UserService userService;
	
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	
	/**
	 * @方法一[现代方式]
	 * @功能 http://localhost:8081/springMVC/test/test01?id=123
	 * 		请求方式:get	
	 * **/
	@RequestMapping(value="/test01",method=RequestMethod.GET)
	public String testSpringmvcOne(@RequestParam("id") String id,Model model){
		User user = userService.getUser(id);
		model.addAttribute(user);
		return "test";
	}
	
	
	/**
	 * @方法二[现代方式]
	 * @功能 http://localhost:8081/springMVC/test/test02/123
	 * 		请求方式:get	
	 * **/
	@RequestMapping(value="/test02/{id}",method=RequestMethod.GET)
	public String testSpringmvcTwo(@PathVariable("id") String id,Map<String,Object> model){
		User user = userService.getUser(id);
		model.put("user", user);
		return "test";
	}
	
	
	/**
	 * @方法三[传统方式]
	 * @功能 http://localhost:8081/springMVC/test/test03?id=123
	 * 		请求方式:get	
	 * **/
	@RequestMapping(value="/test03",method=RequestMethod.GET)
	public String testSpringmvcThree(HttpServletRequest request){
		String id = String.valueOf(request.getParameter("id"));
		User user = userService.getUser(id);
		request.setAttribute("user", user);
		return "test";
	}
	
	
	/**
	 * @方法四[现代方式]
	 * @功能 http://localhost:8081/springMVC/test/admin?add
	 * 		请求方式:get	
	 * **/
	//跳转页面
	@RequestMapping(value="/admin",method=RequestMethod.GET,params="add")
	public String testSpringmvcBindingOne(){
		return "admin/index";
	}
	
	//保持成功之后,重定向
	@RequestMapping(value="/save",method=RequestMethod.POST,params="add")
	public String testSpringmvcBindingTwo(User user){
		userService.add(user);
		//跳转到方法二
		return "redirect:test02/"+user.getId();
	}
	
	
	/**
	 * @方法五 文件上传
	 * @功能 http://localhost:8081/springMVC/test/fileUp
	 * 		请求方式:get	
	 * **/
	@RequestMapping(value="/fileUp",method=RequestMethod.POST)
	public String testSpringmvcOne(@RequestParam("file") MultipathFile file){
		if(!file.isEmpty()){
			System.out.println(file.getOriginalFilename());   
		}		
		return "success";
	}
	
	
	/**
	 * @方法六 json
	 * @功能 http://localhost:8081/springMVC/test/jsonView?id=123
	 * 		请求方式:get	
	 * **/
	@RequestMapping(value="/jsonView",method=RequestMethod.GET)
	public @ResponseBody User testSpringmvcJson(@RequestParam("id") String id){		
		return userService.getUser(id);//返回json
	}


}

springMVC-servlet.xml 配置

<!-- 启动Springmvc注解驱动 -->
    <mvc:annotation-driven/>
 <!-- 返回json 方法一 需要导入 fastjson.jar包 -->  
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="false">
            <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
            <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <!-- 这里顺序不能反,一定先写text/html,不然ie下出现下载提示 -->
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
     
   
  <!-- 返回json 方法二 需要导入 jackson-annotations.jar,jackson-core.jar,jackson-databind.jar-->  
   <!--  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html; charset=UTF-8</value>
                            <value>application/json;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html; charset=UTF-8</value>
                            <value>application/json;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean> -->
 
 
<!-- 其下与返回json 无关  -->
 
    <!-- 自动将控制器加载到bean -->
    <context:component-scan base-package="org.controller"></context:component-scan>    
    <!-- 配置处理静态资源的请求 -->
    <mvc:resources location="/resources/" mapping="*/res/**"/>
     
    <!-- 拦截器配置 -->
    <mvc:interceptors>
        <bean class="org.aop.LoginInteceptor" />
    </mvc:interceptors>
     
    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">    
        <property name="prefix" value="/WEB-INF/jsp/"/>    
        <property name="suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑  -->    
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />    
    </bean>   
     
     <!-- 上传 需要使用到的配置-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="1046666000"/>
    </bean>
     
     <!--配置全局的异常-->
    <!-- <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="com.guanlan.util.SportException">error</prop>
            </props>
        </property>
    </bean> -->


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值