演示一个简单的注册登录过程。
1,首先通过Mybatis自动生成工具生成pojo,mapper.xml和dao
然后写几个jsp页面,比如登录和注册页面:
<form name="form1" method="post">
用户名:<input type="text" name="username"><br>
密码:  <input type="password" name="password"><br>
          
<input type="button" value="登录" οnclick="submit1()">  
<input type="button" value="注册"
οnclick="submit2('${pageContext.request.contextPath}')">
</form>
<script type="text/javascript">
function submit1() {
document.form1.action = "${pageContext.request.contextPath }/login.action";
document.form1.submit();
}
function submit2(value) {
document.form1.action = value + "/registerEdit.action";——跳转到注册页面
document.form1.submit();
}
</script>
这里展示了通过button提交的2种方式,如果用submit只能有一个,而这里有登录和注册,重点部分用粗体标出。
service实现类只有2个方法:查找所有用户及添加用户。
@Service
public class ServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findFromDB() {
return userMapper.selectByExample(null);
}
@Override
public int InsertUser(User u) {
return userMapper.insertSelective(u);
}
}
controller的方法为登录和注册,其余jsp页面随意写点啥:
@Controller
public class UserController {
@Autowired
private UserService us;
@RequestMapping("/home")
public String toHome() {
return "home";
}
@RequestMapping("/registerEdit")
public String toRegister() {
return "register";
}
@RequestMapping("/register")
public String register(String username, String password, String address, String sex) {
List<User> userList = us.findFromDB();
for (User u : userList) {
if (u.getUsername().equals(username) && u.getPassword().equals(password)) {
return "registerFailed";
}
}
User user = new User();
user.setId(userList.size() + 1);
user.setAddress(address);
user.setPassword(password);
user.setSex(sex);
user.setUsername(username);
us.InsertUser(user);
return "registerSuccess";
}
@RequestMapping("/login")
public String login(String username, String password) {
List<User> userList = us.findFromDB();
for (User u : userList) {
if (u.getUsername().equals(username) && u.getPassword().equals(password)) {
return "loginSuccess";
}
}
return "loginFailed";
}
}
下面是2个xml配置文件:
spring-mybatis.xml:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns="http://www.springframework.org/schema/beans"
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">
<context:property-placeholder location="classpath:db.properties" />
<context:component-scan base-package="com.cl.service" />
<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}" />
</bean>
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
这里可以插入configLocations,一般是配置别名的xml,但自动生成的话不用这个配置文件。
<property name="mapperLocations" value="classpath:com/cl/ssm/mapper/UserMapper.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cl.ssm.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean" />
</bean>
</beans>
另一个是spring-mvc.xml文件
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 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">
<context:component-scan base-package="com.cl.controller" />
注解驱动作用是代替手动配置RequestMappingHandlerMapping和RequestMappingHandlerAdapter这2个bean。 <mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
所以上面只需要配置一个视图解析器,设置要跳转的jsp的前缀和后缀,在controller方法里返回字符串时只要返回jsp的不带后缀的名字了。
最后就是web.xml了:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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 ">
加载时初始化配置:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
编码过滤器,防止乱码
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
context加载监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
servlet及初始化配置,过滤规则为带action后缀
<servlet>
<servlet-name>springMVC-zongjie</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>
</servlet>
<servlet-mapping>
<servlet-name>springMVC-zongjie</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
以上就是一个简单的springMVC应用,接触了springMVC后就不想再弄ssm,因为配置真是简单。里面还有很多东西,controller里有Model封存数据的,传输图片或文件(需要配置bean)等,xml中可以配置事务管理器,自定义拦截器、格式转换、异常处理器、servlet监听器等。使用maven要寻找jar包和发布等非常方便,只是要注意jar包冲突(你都不知道咋回事。。。),最好找一份靠谱的jar包集合。