1、idea创建web项目(登录案例),导包,创建mvc包结构。
2、在src下创建并配置ssm的xml文件(applicationcontext.xml,db.properties(略),log4j.properties(略),spring-mvc.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/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"
default-autowire="byName"
>
<!--配置属性文件扫描-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--配置注解扫描-->
<context:component-scan base-package="com.service.impl"></context:component-scan>
<!--配置数据源bean-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${m_driver}"></property>
<property name="url" value="${m_url}"></property>
<property name="username" value="${m_username}"></property>
<property name="password" value="${m_password}"></property>
</bean>
<!--配置工厂bean-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean"></bean>
<!--配置mapper扫描bean-->
<bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="factory"></property>
<property name="basePackage" value="com.mapper"></property>
</bean>
<!--配置事务管理bean-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"></bean>
<!--配置事务管理方法-->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="sel*" read-only="true"/>
<tx:method name="ins*"/>
<tx:method name="up*"/>
<tx:method name="del*"/>
</tx:attributes>
</tx:advice>
<!--配置事务管理切面-->
<aop:config>
<aop:pointcut id="mp" expression="execution(* com.service.impl.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="advice" pointcut-ref="mp"></aop:advisor>
</aop:config>
</beans>
spring-mvc.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.xsd
">
<!--配置注解扫描-->
<context:component-scan base-package="com.controller"></context:component-scan>
<!--配置注解解析器-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置静态资源放行-->
<mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
<mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
<mvc:resources mapping="/images/**" location="/images/"></mvc:resources>
</beans>
3、配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!--配置spring容器的配置文件路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationcontext.xml</param-value>
</context-param>
<!--配置spring的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置springMVC的Servlet-->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置编码过滤器-->
<filter>
<filter-name>code</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>code</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4、实现功能
创建登录页面
<%
String path = request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<base href="<%=basePath%>"/>
<title></title>
</head>
<body>
<form action="UserLogin" >
账号:<input type="text" name="uname" >
密码:<input type="text" name="pwd">
<input type="submit" value="登录">
</form>
</body>
</html>
创建控制器类,并声明处理登录请求的单元方法
@Controller
public class UserController {
@Autowired
private UserService UserService;
@RequestMapping("UserLogin")
public String userLogin(String uname, String pwd){
User user = UserService.checkUserLogin(uname, pwd);
if (user!=null){
return "redirect:/success.jsp";
}else {
return "redirect:/index.jsp";
}
}
}
创建登录业务代码
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User checkUserLogin(String uname, String pwd) {
return userMapper.userLogin(uname, pwd);
}
}
创建登录mapper层代码
public interface UserMapper {
@Select("select * from t_user where uname=#{uname} and pwd=#{pwd}")
User userLogin(@Param("uname") String uid,@Param("pwd") String pwd);
}
- 处理springmvc请求中文乱码:
post请求:在web.xml配置编码过滤器(如上图)
get请求:在Tomcat服务器的配置文件~\conf\server.xml中处理
原:<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
处理:在Connector中加上URIEncoding="UTF-8"
改后: <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>