Hello Struts2\SSH
Hello Struts2
-
创建一个 JavaWeb 应用,Maven 项目,导入相应依赖
-
修改 webapp 目录下的 web.xml 文件,添加拦截器,将所有请求拦截
<?xml version="1.0" encoding="UTF-8"?> <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_4_0.xsd" version="4.0" metadata-complete="true"> <!-- 拦截所有请求交由 structs 处理分发 --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
-
配置 struts.xml 文件,配置相应的 action
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="login" extends="struts-default" namespace="/"> <action name="login" class="cn.qh.LoginAction" method="login"> <result name="success">success.jsp</result> <result name="error">error.jsp</result> </action> </package> </struts>
-
编写相应的 Java 类处理
// 已略去 getter、setter 等方法 public class LoginAction extends ActionSupport { private static final String USER_NAME = "admin"; private static final String PASSWORD = "123456"; private String userName; private String password; public String login() { if (getUserName().equals(USER_NAME) && getPassword().equals(PASSWORD)) { return "success"; } else { return "error"; } } }
-
处理页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登录</title> </head> <body> <form action="./login" method="post"> <input type="text" name="userName" placeholder="请输入用户名"/><br> <input type="password" name="password" placeholder="请输入密码"/><br> <button type="submit">登录</button> </form> </body> </html>
-
结果展示页面
<!-- 密码错误 --> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>密码错误</title> </head> <body> <h1>密码错误</h1> </body> </html> <!-- 密码正确 --> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登录成功</title> </head> <body> <h1>登录成功</h1> </body> </html>
Hello SSH
-
展示页面基于上述 Hello Struts2,struts.xml 文件无需修改
-
导入相应 Maven 依赖后,在 web.xml 文件中导入 Spring 上下文
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" <!-- ......... 省略部分的配置同实验三 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext.xml</param-value> </context-param> </web-app>
-
Spring 配置文件
<!-- Spring 总配置文件 --> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 导入各层的配置文件 --> <import resource="applicationContext-mapper.xml"/> <import resource="applicationContext-service.xml"/> <import resource="applicationContext-action.xml"/> </beans> <!-- Controller 层 --> <?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" 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:component-scan base-package="cn.qh.action"/> </beans> <!-- Service 层 --> <?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" 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:component-scan base-package="cn.qh.service"/> </beans>
接入持久层 Hibernate,在 applicationContext-mapper.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" 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="cn.qh.mapper"/> <bean id="dataSource" class="org.apache.commons.dbcp2.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="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:hibernate/hibernate.cfg.xml"/> <!-- //加载实体类的映射文件位置及名称 --> <property name="mappingLocations" value="classpath:hibernate/*.hbm.xml"/> </bean> <bean class="org.springframework.orm.hibernate3.HibernateTemplate" id="hibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"/> </bean> </beans>
数据库连接信息来源于 db.properties
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/stu?serverTimezone=GMT%2B8 jdbc.username=root jdbc.password=123456
Hibernate 总配置文件 hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 配置Hibernate的基本属性 --> <!-- 1.数据源配置到IOC容器中 --> <!-- 2.关联的.hbm.xml也在IOC容器配置SessionFactory实例 --> <!-- 3.配置Hibernate的基本属性:方言,SQL显示及格式化,生成数据表的策略以及二级缓存 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="hibernate.show_sql">true</property> <property name="hbm2ddl.auto">update</property> </session-factory> </hibernate-configuration>
数据库 orm 映射关系对应存储位置 classpath:hibernate/*.hbm.xml,具体映射配置如下图所示
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.qh.pojo.User" table="user"> <id name="id" type="java.lang.Integer"> <column name="id"/> <generator class="increment"/> </id> <property name="username" type="java.lang.String"> <column name="username" /> </property> <property name="password" type="java.lang.String"> <column name="password" /> </property> </class> </hibernate-mapping>
对应的 JavaBean
@Data @Builder @NoArgsConstructor @AllArgsConstructor public class User { private Integer id; private String username; private String password; }
-
数据库持久层 mapper 类
@Repository public class UserMapperImpl implements UserMapper { @Autowired private HibernateTemplate hibernateTemplate; @Override @SuppressWarnings("all") public User selectUserInfoByUsername(String username) { User user = User.builder().username(username).build(); List<User> list = hibernateTemplate.findByExample(user); return (list == null || list.size() == 0) ? null : list.get(0); } }
-
业务层
@Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public Boolean login(String username, String password) { User user = userMapper.selectUserInfoByUsername(username); if (user == null) { return false; } return user.getPassword().equals(password); } }
-
Controller 层
@Controller @Data public class UserAction { @Autowired private UserServiceImpl userServiceImpl; private String username; private String password; public String login() { if (userServiceImpl.login(getUsername(), getPassword())) { return "success"; } else { return "error"; } } }
-
将 struts.xml 文件中的 action 绑定到上述 Controller 即可完成链接
上述代码中用到的所有依赖及版本
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.12</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>2.5.12</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5.12</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.9.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
</dependencies>