发表于: 2018-03-21 23:17:35

1 646


今天完成的事情:

1.搭建SSM框架。

1.1web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        version="3.1">
 <display-name>ChatRobot</display-name>
 <description>ChatRobot_Alpha_0.0.1</description>
 <!-- 编码过滤器 -->
 <filter>
   <filter-name>encodingFilter</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>encodingFilter</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>
 <!-- 配置DispatcherServlet -->
 <servlet>
   <servlet-name>SpringMVC</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <!-- 配置springMVC需要加载的配置文件-->
   <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:spring-*.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
   <async-supported>true</async-supported>
 </servlet>
 <servlet-mapping>
   <servlet-name>SpringMVC</servlet-name>
   <!-- 匹配所有请求,此处也可以配置成 *.do 形式 -->
   <url-pattern>/</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
   <welcome-file>index.html</welcome-file>
 </welcome-file-list>
</web-app>


2.spring-mybatis.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:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://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/tx
      http://www.springframework.org/schema/tx/spring-tx.xsd">
   <!-- 扫描service包下所有使用注解的类型 -->
   <context:component-scan base-package="com.chatRobot.service"/>
   <!-- 配置数据库相关参数properties的属性:${url} -->
   <context:property-placeholder location="classpath:jdbc.properties"/>
   <!-- 数据库连接池 -->
   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
       <property name="driverClass" value="${jdbc.driver}"/>
       <property name="jdbcUrl" value="${jdbc.url}"/>
       <property name="user" value="${jdbc.username}"/>
       <property name="password" value="${jdbc.password}"/>
       <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
       <property name="minPoolSize" value="${c3p0.minPoolSize}"/>
       <property name="autoCommitOnClose" value="${c3p0.autoCommitOnClose}"/>
       <property name="checkoutTimeout" value="${c3p0.checkoutTimeout}"/>
       <property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}"/>
   </bean>
   <!-- 配置SqlSessionFactory对象 -->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <!-- 注入数据库连接池 -->
       <property name="dataSource" ref="dataSource"/>
       <!-- 扫描model包 使用别名 -->
       <property name="typeAliasesPackage" value="com.chatRobot.model"/>
       <!-- 扫描sql配置文件:mapper需要的xml文件 -->
       <property name="mapperLocations" value="classpath:mapper/*.xml"/>
   </bean>
   <!-- 配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <!-- 注入sqlSessionFactory -->
       <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
       <!-- 给出需要扫描Dao接口包 -->
       <property name="basePackage" value="com.chatRobot.dao"/>
   </bean>
   <!-- 配置事务管理器 -->
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <!-- 注入数据库连接池 -->
       <property name="dataSource" ref="dataSource"/>
   </bean>
   <!-- 配置基于注解的声明式事务 -->
   <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

3.spring-mvc.xml

<!-- 扫描web相关的bean -->
<context:component-scan base-package="com.chatRobot.controller"/>
<!-- 开启SpringMVC注解模式 -->
<mvc:annotation-driven/>
<!-- 静态资源默认servlet配置 -->
<mvc:default-servlet-handler/>
<!-- 配置jsp 显示ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
   <property name="prefix" value="/WEB-INF/views/"/>
   <property name="suffix" value=".jsp"/>
</bean>

4.UserController.java

@Controller
@RequestMapping("/user")
public class UserController
@Resource
   private IUserService userService;
   @RequestMapping("/showUser.do")
public void selectUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
request.setCharacterEncoding("UTF-8");
       response.setCharacterEncoding("UTF-8");
       long userId = Long.parseLong(request.getParameter("id"));
       User user = this.userService.selectUser(userId);
       ObjectMapper mapper = new ObjectMapper();
       response.getWriter().write(mapper.writeValueAsString(user));
       response.getWriter().close();
  
}

5.index.html

<!DOCTYPE html>
<html>
<head>
   <meta charset="UTF-8">
   <title>test</title>
</head>
<script>
   function selectUser() {
var xmlhttp = new XMLHttpRequest();
       xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("test").innerHTML = xmlhttp.responseText;
           }
}
xmlhttp.open("POST", "user/showUser.do", true);
       xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       xmlhttp.send("id=1");
   }
</script>
<body>
<p id="test">Hello World!</p>
<button type="button" onclick="selectUser()">onclick test</button>
</body>
</html>

明天的计划:

搭建自己的SSM框架

遇到的问题:

配置xml文件比较混乱,理清挺难的。

收获:

按教程搭建SSM框架。还需要学很多东西。


返回列表 返回列表
评论

    分享到