SSH与SSM简介
SSM:Spring+SpringMVC+Mybatis
SSH:Struts2+Hibernate+Spring
Struts2:是侧重于控制层的框架
Hibernate:是一个ORM(Object Relation Mapping)的产品,专职于做DAO层
Spring:是一个项目的大管家或者是大容器。它负责各个层次的融合
为什么要用框架来开发项目?
1.简化开发流程
2.标准化开发流程
3.增强项目的扩展性和维护性
Struts2的开发流程:
1.新建web项目,加入struts2的核心jar包
2.在项目中的web.xml中加入struts2的核心过滤器
<?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_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>struts2_06</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <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> </web-app>
3.在src目录下创建一个struts.xml的配置文件
4.在src目录下新建Action普通类,在类中写一个public String execute() 返回一个字符串
package com.action; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ public String execute(){ System.out.println("我的struts2第一个action"); return "success"; } }
5.在struts.xml中完成Action的注册配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="my" namespace="/" extends="struts-default"> <action name="login" class="com.action.LoginAction"> <result name="success">/jsp/success.jsp</result> </action> </package> </struts>
注意:name="login"是指action的访问路径
<result name="success">/jsp/success.jsp</result>:是返回后的跳转结果
6.login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="login"> 用户名:<input name="username"><br> 密码:<input type="password" name="pwd"><br> <input type="submit" value="提交"> </form> </body> </html>
点击提交即可跳转至success.jsp页面