1. 关于web.xml的配置
* xml头配置
* <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" version="4.0">
*内容`
<display-name>test</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>
<!-- 解决乱码问题 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 设置编码参是UTF8 -->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/springcontext.xml</param-value>
</context-param>
<!-- 使用监听器加载Spring配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 添加前置控制器Servlet配置代码 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定SpringMVC配置文件 -->
<!-- SpringMVC的配置文件的默认路径是/WEB-INF/${servlet-name}-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
2.关于springmvc的配置
*springmvc头配置
<?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:p="http://www.springframework.org/schema/p"
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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
*配置内容
<!-- 配置controller扫描包 -->
<context:component-scan base-package="cn.cetc.ssm.controller"/>
<!-- 配置servicer扫描包 -->
<context:component-scan base-package="cn.cetc.ssm.service"/>
<!-- 注解驱动 -->
<mvc:annotation-driven/>
<!-- 访问静态资源文件 -->
<!-- 文件若放在静态路径/statics下,可以直接访问,而不经过controller -->
<mvc:resources mapping="/statics/**" location="/statics/"/>
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置逻辑视图的前缀 -->
<property name="prefix" value="/WEB-INF/page/" />
<!-- 配置逻辑视图的后缀 -->
<property name="suffix" value=".jsp" />
</bean>
3.关于springcontext 的配置
*头部
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.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
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
*主体
<!-- 加载配置文件 -->
<context:property-placeholder location="/WEB-INF/config/db.properties" />
<!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="/WEB-INF/config/sqlMapConfig.xml"/>
</bean>
<!-- 配置Mapper扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置Mapper扫描包 -->
<property name="basePackage" value="cn.cetc.ssm.mapper" />
</bean>
<!-- 注解事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启注解 -->
<tx:annotation-driven/>
4.关于sqlMapConfig的配置
所有**
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
5.一:前端控制器 Controller的配置
@Controller
@RequestMapping("/energy")
public class IndexController {
@RequestMapping(value = "/index")
public ModelAndView index(HttpServletRequest request,
HttpServletResponse response, ModelMap model) {
ModelAndView mv = new ModelAndView();
mv.setViewName("Index");
return mv;
}
@RequestMapping(value = "/toLogin")
public ModelAndView toLogin(HttpServletRequest request,
HttpServletResponse response, ModelMap model) {
ModelAndView mv = new ModelAndView();
mv.setViewName("Login");
return mv;
}
@Autowired
private UserService userService;
@RequestMapping("/login")
public void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
User user = userService.getUserByUsername(username);
String result = "";
if (user != null && password.equals(user.getPassword())) {
req.getSession().setAttribute("user", user);
result = new Gson().toJson("true");
} else {
result = new Gson().toJson("false");
}
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html");
String jsonp = req.getParameter("jsoncallback");
if (jsonp != null) {
result = jsonp + "(" + result + ")";
}
resp.getWriter().write(result);
}
@RequestMapping(value = "/toRegister")
public ModelAndView toRegister(HttpServletRequest request,
HttpServletResponse response, ModelMap model) {
ModelAndView mv = new ModelAndView();
mv.setViewName("Register");
return mv;
}
@RequestMapping("/saveUser")
public void saveUser(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException, ParseException {
String username = req.getParameter("username");
User u = userService.getUserByUsername(username);
String result = null;
if (u != null) {
result = new Gson().toJson("false");
} else {
String password = req.getParameter("password");
String nickname = req.getParameter("nickname");
int sex = Integer.parseInt(req.getParameter("val"));
int usertype = Integer.parseInt(req.getParameter("item"));
String bdate = req.getParameter("birthdate");
String dlt = req.getParameter("dltime");
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date birthdate = null;
Date dltime = null;
try {
birthdate = format.parse(bdate);
dltime = format.parse(dlt);
} catch (ParseException e) {
e.printStackTrace();
}
User user = new User();
String id = UUID.randomUUID().toString();
user.setId(id);
user.setUsername(username);
user.setPassword(password);
user.setCreatetime(new Date());
user.setNickname(nickname);
user.setBirthdate(birthdate);
user.setSex(sex);
user.setDltime(dltime);
user.setUsertype(usertype);
userService.saveUser(user);
result = new Gson().toJson(user);
}
String jsonp = req.getParameter("jsoncallback");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html");
if (jsonp != null) {
result = jsonp + "(" + result + ")";
resp.getWriter().write(result);
} else {
resp.getWriter().write(result);
}
}
6.关于UserMapper。xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.cetc.ssm.mapper.UserMapper">
<resultMap type="cn.cetc.ssm.pojo.User" id="user">
<id column="id" property="id" />
<result column="username" property="username" />
<result column="password" property="password" />
<result column="createtime" property="createtime" />
<result column="nickname" property="nickname" />
<result column="birthdate" property="birthdate" />
<result column="sex" property="sex" />
<result column="dltime" property="dltime" />
<result column="hpic" property="hpic" />
<result column="usertype" property="usertype" />
</resultMap>
<select id="getUserByUsername" parameterType="java.lang.String"
resultType="cn.cetc.ssm.pojo.User">
select * from user where username = #{username}
</select>
</mapper>