web服务器:tomcat
暂未采用hibernate处理,基于myeclipse10,配置spring框架
1.首先在web.xml配置监听器,例如ServletContextListener可在tomcat启动时进行加载,进行一些需要的工作,例如tcp连接的监听;配置过滤器(可配置启动的参数),例如编码过滤,
<listener>
<listener-class>
com.didi.community.listener.AppServletContextListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value> //上下文配置xml
</context-param>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup> //表示启动优先级
</servlet>
<filter>
<filter-name>encoding</filter-name>
<filter-class>com.didi.community.listener.EncodingFilter</filter-class> //编码过滤具体类
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value> //采用UTF-8编码
</init-param>
</filter>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name> //servlet映射,对于所有的.html请求均由dispatcherServlet-servlet.xml配置的属性进行对应分发;
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<filter-mapping>
<filter-name>encoding</filter-name> //所有请求均进行编码过滤;
<url-pattern>/*</url-pattern>
</filter-mapping>
配置的applicationContext.xml 主要配置数据库的属性
<beans>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/数据库名</value>
</property>
<property name="username">
<value>数据库登录用户</value>
</property>
<property name="password">
<value>数据库登录密码</value>
</property>
</bean>
<!-- 配置JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
<!-- 配置dao -->
<bean id="userDao" class="com.didi.community.dao.UserDao"> /字段对应类
<property name="jdbcTemplate"> //涉及到的字段,与上面的相关联
<ref local="jdbcTemplate"/>
</property>
</bean>
<bean id="loginLogDao" class="com.didi.community.dao.LoginLogDao">
<property name="jdbcTemplate">
<ref local="jdbcTemplate"/>
</property>
</bean>
<bean id="orderDao" class="com.didi.community.dao.OrderDao">
<property name="jdbcTemplate">
<ref local="jdbcTemplate"/>
</property>
</bean>
<bean id="userService" class="com.didi.community.service.UserService"> 配置UserService类的操作对象字段 userService,具体操作时根据此id取出进行操作;
<property name="userDao"> //涉及到的字段
<ref local="userDao"/>
</property>
<property name="loginLogDao">
<ref local="loginLogDao"/>
</property>
<property name="orderDao">
<ref local="orderDao"/>
</property>
</bean>
</beans>
dispatcherServlet-servlet.xml配置
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="api/login.html">login</prop> //配置相应请求的处理id
<prop key="api/registe.html">registe</prop>
<prop key="api/authenty.html">authenty</prop>
<prop key="api/system.html">system</prop>
<prop key="api/order.html">order</prop>
</props>
</property>
</bean>
<bean id="login" class="com.didi.community.controller.LoginController"></bean> //id对应的处理类
<bean id="registe" class="com.didi.community.controller.RegisteController"></bean>
<bean id="authenty"class="com.didi.community.controller.AuthentyController"></bean>
<bean id="system" class="com.didi.community.controller.SystemController"></bean>
<bean id="order" class="com.didi.community.controller.OrderController"></bean>
对于处理表单和json请求:
可采取先判断是否为表单请求,boolean isHaveData = ServletFileUpload.isMultipartContent(request);如果非表单,在
StringBuilder builder = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(arg0.getInputStream(),"utf-8"));
String temp;
while ((temp = br.readLine()) != null) {
builder.append(temp);
}
br.close();
temp = builder.toString();
handleJsonRequest(temp);
可对上传的json数据进行解析进行相应操作;即可满足app的json请求/表单,也可满足web端表单