1. 首先创建一个Dynamic Web Project,名为SSM_Example
在WEB_INF目录下创建web.xml
SSM_Example
login.jsp
2. 把struts2所需的jar包放到工程的WEB-INF\lib目录下
所需的jar包有:
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang-2.4.jar
commons-lang3-3.1.jar
commons-logging-1.1.3.jar
commons-logging-api-1.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-core-2.3.16.3.jar
xwork-core-2.3.16.3.jar
/jsp/home.jsp
login.jsp
在web.xml中添加如下代码
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
在WebContent根目录创建login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Welcome to login page
login page
在WebContent目录下创建jsp文件夹,在文件夹里创建home.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Welcome to home page
home page
在src中创建com.ssm.action包,在包下创建LoginAction类,LoginAction类继承ActionSupport
package com.ssm.action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
import com.ssm.dto.User;
import com.ssm.service.ILoginService;
@Controller
public class LoginAction extends ActionSupport {
/**
* serialVersionUID.
*/
private static final long serialVersionUID = 1L;
private String userName;
private String password;
public void setUserName(String userName) {
this.userName = userName;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
3. 配置Spring,把Spring的jar包放在lib目录下:
aopalliance-1.0.jar
spring-aop-3.0.2.RELEASE.jar
spring-beans-3.0.5.RELEASE.jar
spring-context-3.0.5.RELEASE.jar
spring-core-3.0.5.RELEASE.jar
spring-expression-3.0.5.RELEASE.jar
spring-jdbc-3.0.5.RELEASE.jar
spring-tx-3.0.5.RELEASE.jar
spring-web-3.0.5.RELEASE.jar
struts2-spring-plugin-2.3.16.3.jar
在WebContent下创建config/applicationContext.xml文件
在web.xml中添加
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
/config/applicationContext.xml
commons-dbcp2-2.0.1.jar
commons-pool2-2.2.jar
mybatis-3.2.6.jar
mybatis-spring-1.2.2.jar
mysql-connector-java-5.1.13.jar
package com.ssm.dto;
public class User {
private String userName;
private String password;
private int userType;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getUserType() {
return userType;
}
public void setUserType(int userType) {
this.userType = userType;
}
}
package com.ssm.dao;
import com.ssm.dto.User;
public interface UserMapper {
User getUser(String userName);
}
package com.ssm.service;
import com.ssm.dto.User;
public interface ILoginService {
User Login(String userName);
}
package com.ssm.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ssm.dao.UserMapper;
import com.ssm.dto.User;
@Service
public class LoginServiceImpl implements ILoginService {
@Autowired
UserMapper userMapper;
@Override
public User Login(String userName) {
return userMapper.getUser(userName);
}
}
@Autowired
ILoginService loginServiceImpl;
@Override
public String execute() throws Exception {
User user = loginServiceImpl.Login(userName);
if(null != user){
if(user.getPassword().equals(password)){
return SUCCESS;
}else{
return ERROR;
}
}else{
return ERROR;
}
}
在页面中输入正确的UserName和Password就能访问到home page页面,否则页面不跳转。
5. 项目创建的目录结构如下