Spring MVC

本文介绍了Spring MVC的配置步骤,包括导入相关包,并在WebContent/WEB-INF下的conf文件夹中创建了applicationContext.xml、applicationContext-bizc.xml和applicationContext-dao.xml等配置文件,用于分离不同层次的组件配置。
摘要由CSDN通过智能技术生成

1、导包 下载相关的包导入,下载链接:http://download.csdn.net/detail/gufengpiaoyi/9285145

2、WebContent/WEB-INF下新建conf文件夹,新建applicationContext.xml,applicationContext-bizc.xml,applicationContext-dao.xml三个xml文件:

applicationContext.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: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-3.0.xsd">
          
    <context:component-scan  base-package="com.controller" />  
	<mvc:annotation-driven />
    <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>
	
    
</beans>

applicationContext-bizc.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:context="http://www.springframework.org/schema/context"  
    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">  
        
	<bean id="dataBizc" class="com.bizc.impl.DataBizcImpl">
		<property name="dataDao" ref="dataDao"></property>
	</bean>
	
</beans>

applicationContext-dao.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:context="http://www.springframework.org/schema/context"  
    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">  
        
	<bean id="dataDao" class="com.dao.impl.DataDaoImpl" />
	
</beans>

3、src文件夹下新建六个包:com.bizc方法层,com.bizc.impl方法实现层(由com.dao.impl实现),com.dao数据库方法层(根据bizc写),com.dao.impl数据库方法实现层,com.contoller控制层,com.po实例层。

controller:

@Controller
@RequestMapping(value= "/getdata",method=RequestMethod.POST)
public class BDController {
	@Autowired
	private IDataBizc dataBizc;
	@RequestMapping(value="/getminutedata")
	public @ResponseBody WrappedResult getMinuteData(@RequestParam String ItemNO){
		
		try {
			return WrappedResult.successWrapedResult(dataBizc.getMinuteData(ItemNO));
		} catch (ClassNotFoundException e) {
			return WrappedResult.failedWrappedResult("数据库驱动类加载错误!");
		} catch (SQLException e) {
			return WrappedResult.failedWrappedResult("数据库查询失败!");
		}
	}
	@RequestMapping(value="/getrealtimedata")
	public @ResponseBody WrappedResult getRealTimeDataNew(){
		try {
			return WrappedResult.successWrapedResult(dataBizc.getRealTimeDataNew());
		} catch (ClassNotFoundException e) {
			return WrappedResult.failedWrappedResult("数据库驱动类加载错误!");
		} catch (SQLException e) {
			return WrappedResult.failedWrappedResult("数据库查询失败!");
		}
	}
	@RequestMapping(value="/getbatterydata")
	public @ResponseBody WrappedResult getBatteryData(){
		try {
			return WrappedResult.successWrapedResult(dataBizc.getBatteryData());
		} catch (ClassNotFoundException e) {
			return WrappedResult.failedWrappedResult("数据库驱动类加载错误!");
		} catch (SQLException e) {
			return WrappedResult.failedWrappedResult("数据库查询失败!");
		}
	}
	public IDataBizc getDataBizc() {
		return dataBizc;
	}
	public void setDataBizc(IDataBizc dataBizc) {
		this.dataBizc = dataBizc;
	}
	
}

返回json标准化:

public class WrappedResult {

	public WrappedResult() {

	}

	/**
	 * 是否成功
	 */
	private boolean successful;
	/**
	 * 业务方法返回数据
	 */
	private Object resultValue;
	/**
	 * 提示信息
	 */
	private String resultHint;

	/**
	 * 成功调用返回包装结果
	 * 
	 * @param data 要展示的数据{"items":[{},{}]}
	 */
	public static WrappedResult successWrapedResult(Object data) {
		return new WrappedResult(true, data, "");
	}

	/**
	 * 失败调用返回包装结果
	 * 
	 * @param exMessage
	 *            异常信息
	 */

	public static WrappedResult failedWrappedResult(String exMessage) {
		return new WrappedResult(false, "", exMessage);
	}

	/**
	 * 
	 * @param isSuccess 请求是否成功
	 * @param data 请求返回的数据
	 * @param tip 请求失败时显示异常信息
	 */
	public WrappedResult(boolean isSuccess, Object data, String tip) {
		super();
		this.successful = isSuccess;
		this.resultValue = data;
		this.resultHint = tip;
	}

	public boolean isSuccessful() {
		return successful;
	}

	public void setSuccessful(boolean successful) {
		this.successful = successful;
	}

	public Object getResultValue() {
		return resultValue;
	}

	public void setResultValue(Object resultValue) {
		this.resultValue = resultValue;
	}

	public String getResultHint() {
		return resultHint;
	}

	public void setResultHint(String resultHint) {
		this.resultHint = resultHint;
	}

}

4、配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>BdForestGuard</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>  
			/WEB-INF/conf/applicationContext-*.xml  
		</param-value>
  </context-param>
  
  <servlet>
    <servlet-name>rest</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/conf/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>rest</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值