`
jie66989
  • 浏览: 251002 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

spring mvc学习

阅读更多
web.xml配置spring mvc的servlet
<!-- 配置spring mvc的相关内容,此处的servlet-name任意,但必须有<your servlet-name>-servlet.xml与之对应 -->
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
	<!-- 配置对应以.do或者action结尾的请求,默认为2者都可以 -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>
<!-- 配置过滤器,同时把所有的请求都转为utf-8编码 -->
	<filter>
		<filter-name>Spring character encoding filter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>Spring character encoding filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

上面对应的spring配置文件为WEB-INF/spring-servlet.xml
这里分两种配置
1.简单配置
<!-- 对定义包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
	<context:component-scan base-package="com.abc" />
	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 这个可以不用-->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
	<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
        p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/> 

测试类:
import com.baobaotao.service.BbtForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
// @RequestMapping("/a")// 如果有此项配置,则请求下面方法的时候需要加上/a/路径
// 这里需要注意的是:如果给类上面不添加RequestMapping注解,那么在方法上添加的时候,此方法必须为共有方法,不能为private,如果添加类注解就不受此限制
public class BbtForumController {
    //@Autowired//或者@Resource都可以注入操作的bean
    //private BbtForumService bbtForumService;

    @RequestMapping("/listAllBoard.do") // /listAllBoard.do 的 URL 请求将由 listAllBoard() 负责处理
    public String listAllBoard() {
        System.out.println("call listAllBoard method.");
        return "listBoard";// 返回WEB-INF/jsp/listBoard.jsp页面
    }

    // 如果URL请求中包括"method=listAllBoard"的参数,由本方法进行处理,并指定http请求的方式为GET
// 当请求/bbtForum.do?method=listBoardTopic&topicId=1时候会将url参数topicId传给处理方法listBoardTopic的参数topicId中   添加@RequestParam("id") 在方法参数int topicId前将用id和url中的参数进行绑定
    @RequestMapping(@RequestMapping(params = "method=listAllBoard") ,method=RequestMethod.GET)
    public String listBoardTopic(int topicId) {
        System.out.println("call listBoardTopic method.");
        return "listTopic";
    }
}

参考:http://wenku.baidu.com/view/11914508581b6bd97f19ea78.html

1.高级配置
<!-- 对定义包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
	<context:component-scan base-package="com.abc" />

<!-- 上传文件时需要用到的分解器,默认将编码转为utf-8 
	<bean id="multipartResolver"  
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <property name="defaultEncoding">
        	<value>UTF-8</value>
        </property>
    </bean>  
-->
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 
	-->
	<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">  
		<property name="mediaTypes">  
		  <map>  
		    <entry key="html" value="text/html"/>  
		    <entry key="xml" value="application/xml"/>
		    <entry key="json" value="application/json"/>  
		  </map>  
		</property>
		<property name="viewResolvers">  
		  <list>
		    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
		      <property name="prefix" value="/jsp/"/>
		      <property name="suffix" value=".jsp"/>
		    </bean>
		  </list>
		</property>
		<property name="defaultViews">
			<list>
				<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
			</list>
		</property>
	</bean>
	<bean  
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
<!--   
      配置信息转换,将用@responsebody注解的返回值转换为json返回前台,编码为utf-8
      -->  
        <property name="messageConverters">  
            <list>  
                <bean  
                    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
                    <property name="supportedMediaTypes">  
                        <list>  
                            <value>application/json;charset=UTF-8</value>  
                        </list>  
                    </property>  
                </bean>  
            </list>  
        </property>  
    </bean>

测试类:
@Controller
@RequestMapping("/command_service")
public class CommandService {
	@Resource
	private Resp resp;
	@Resource
	private ICommandDAO commandDAOImpl;

	@RequestMapping(value="/add_command",method=RequestMethod.POST)
	@ResponseBody//这里应该意思就是返回json格式
	public Resp addCommand(@RequestBody Command command){
		try{
			commandDAOImpl.addNewCommand(command);
			resp.setData("");
			resp.setResp_code(Resp.SUCCESS);
			resp.setResp_msg("");
			return resp;
		}catch(Exception e){
			e.printStackTrace();
			resp.setData("");
			resp.setResp_code(Resp.FAILD);
			resp.setResp_msg(messageResource.getMessage("add_command_error",null, Locale.getDefault()));
			return resp;
		}
	}
}

public class Resp {
	public static final int FAILD=0;
	public static final int SUCCESS=1;
	
	private Object data;
	private int resp_code;
	private String resp_msg;
	public int getResp_code() {
		return resp_code;
	}
	public void setResp_code(int resqCode) {
		resp_code = resqCode;
	}
	public String getResp_msg() {
		return resp_msg;
	}
	public void setResp_msg(String resqMsg) {
		resp_msg = resqMsg;
	}

	public void setData(Object data) {
		this.data = data;
	}
	public Object getData() {
		return data;
	}
}


获取request对象
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder
			      .getRequestAttributes()).getRequest();

地址:http://jie66989.iteye.com/blog/1700252
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics