SSH简介
Struts+Spring+Hibernate
Struts2介绍
Struts2是apache组织推广和维护。Struts2和Struts1没有必然联系,是将webwork框架内核(xwork)进行封装和改造,命名为Struts2推广。
Struts2是基于MVC思想设计的框架,例如SpringMVC、Struts1、WebWork、JSF等。
Struts2的实现结构
spring的实现结构
struts2的实现结构
Struts2基本使用
搭建struts2开发环境使用HelloAction进行入门。
创建war类型的maven项目。生成web.xml。
1.jar包导入
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.13</version>
</dependency>
</dependencies>
2.在resources文件夹下引入struts.xml配置文件
<?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>
</struts>
3.案例流程设计
/hello.action-->StrutsPrepareAndExecuteFilter-->Action-->Result-->/hello.jsp-->生成HTML响应界面
4.在web.xml配置Filter控制器
<filter>
<filter-name>strutsmvc</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>strutsmvc</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
5.编写Action
package cn.xdl.action;
/**
* 1.方法修饰符public
* 2.返回类型为String
* 3.方法名execute,改名需要增加额外XML配置
* 4.方法参数无参
*/
public class HelloAction {
public String execute() {
System.out.println("进入了HelloAction处理");
return "hello";//对应<result name="hello">
}
}
6.在struts.xml配置Action
7.在struts.xml配置Result
<?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="demo1" extends="struts-default" namespace="/">
<!-- name指定请求名;class指定 Action类;method指定业务方法名,默认为execute-->
<action name="hello" class="cn.xdl.action.HelloAction" method="execute">
<!-- name对应action返回值 ;type指定result组件类型,默认dispatcher-->
<result name="hello" type="dispatcher">
<param name="location">/hello.jsp</param>
</result>
</action>
</package>
</struts>
8.在src下webapp文件夹下新建编写hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Struts2入门实例</h1>
</body>
</html>
测试:启动tomcat。
输入:http://localhost:8080/ssh01/hello.action
后台:
--------------------------------------------------------------------------------------------
指定访问后缀为.do方式并且加上一层/ssh
http://localhost:8080/ssh01/ssh/hello.do这种形式
修改web.xml的<url-pattern>*.do</url-pattern>和struts.xml文件中<package name="demo1" extends="struts-default" namespace="/ssh">
若*.do改为/*则http://localhost:8080/ssh01/ssh/hello和http://localhost:8080/ssh01/ssh/hello.action都能管用。
但执意要使用.do请求则需要在struts.xml文件加上
<!-- 指定请求扩展名 -->
<constant name="struts.action.extension" value="action,do,,"></constant>
如此才能实现访问后缀的方式为.do
万能配适合:
<url-pattern>/*</url-pattern> ----->web.xml
<constant name="struts.action.extension" value="action,do,,"></constant>支持三种后缀。--->struts.xml
http://localhost:8080/ssh01/ssh/hello.action http://localhost:8080/ssh01/ssh/hello http://localhost:8080/ssh01/ssh/hello.do
参数的传递
不需要像spring那样传给session或request对象。
public class HelloAction {
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String execute() {
msg="struts2参数传递";
System.out.println("进入了HelloAction处理");
return "hello";//对应<result name="hello">
}
}
在hello.jsp页面直接使用EL表达式<h2>${msg}</h2>使用msg就行。
引入一个列表,即实体类的引入。
写一个entity实体类。
package cn.xdl.entity;
public class Dept {
private int no;
private String name;
public Dept() {
super();
}
public Dept(int no, String name) {
super();
this.no = no;
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在HelloAction中加入
package cn.xdl.action;
import java.util.ArrayList;
import java.util.List;
import cn.xdl.entity.Dept;
/**
* 1.方法修饰符public
* 2.返回类型为String
* 3.方法名execute,改名需要增加额外XML配置
* 4.方法参数无参
* @author 朱亚孟
*/
public class HelloAction {
private List<Dept> depts;
public List<Dept> getDepts() {
return depts;
}
public void setDepts(List<Dept> depts) {
this.depts = depts;
}
public String execute() {
depts = new ArrayList<Dept>();
depts.add(new Dept(10,"JAVA"));
depts.add(new Dept(20,"JAVA"));
depts.add(new Dept(30,"JAVA"));
System.out.println("进入了HelloAction处理");
return "hello";//对应<result name="hello">
}
}
再hello.jsp使用EL表达式${depts}使用depts变量。
运行:
也可以加入jstl标签库,jstl的jar包。
显示列表。
----------------------------------------------------------------------------------------------------------------------------------------------
登录案例
1.流程设计
/login.do-->StrutsPrepareAndExecuteFilter-->Action-->Result-->成功ok.jsp/失败login.jsp
2.在web.xml配置Filter控制器(采用万能后缀/*的方式)
<filter>
<filter-name>strutsmvc</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>strutsmvc</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.编写LogionAction(提供了三种获取session对象的操作)
package cn.xdl.action;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
public class LoginAction implements SessionAware{
private String username;//<input type="text" name="username">
private String password;//<input type="password" name="password">
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
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 String execute() {
//判断账号密码是否正确
if ("scott".equalsIgnoreCase(username)
&&"1234".equalsIgnoreCase(password)) {
//1.获取Map类型session对象
//Map<String, Object> session = ActionContext.getContext().getSession();
//session.put("user", username);
//2.获取Servlet类型的session对象
//HttpSession session = ServletActionContext.getRequest().getSession();
//session.setAttribute("user", username);
//3.采用Aware接口方法注入的session
session.put("user", username);
//获取request对象
//Map<String, Object> request = (Map)ActionContext.getContext().get("request");
return "success";
}else {
msg="账号或密码错误";
return "error";
}
}
private Map<String, Object> session;
//创建Action对象时自动执行
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
}
4.在struts.xml配置Action
在struts.xml配置Result(简写的方式)
<package name="demo2" extends="struts-default">
<action name="login" class="cn.xdl.action.LoginAction">
<result name="success">/ok.jsp</result>
<result name="error">/login.jsp</result>
</action>
</package>
5.编写ok.jsp和login.jsp
//login.jsp
<body>
<div style="color: red;">${msg}</div>
<form action="login.do" method="post">
账号:<input type="text" name="username"><br/>
密码:<input type="password" name="password"><br/>
<input type="submit" value="登录">
</form>
</body>
//ok.jsp
<body>
<h1>登录成功,欢迎${sessionScope.user }访问</h1>
<%=session.getAttribute("user") %>
<a href="login.jsp">重新登录</a>
</body>
结果:发出的action就是login.do
注意:为了方便session的统一管理可以写一个BaceAction父类统一处理request、session、application应用对象。
package cn.xdl.action;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
public class BaseAction implements SessionAware,RequestAware,ApplicationAware
,ServletRequestAware,ServletResponseAware,ServletContextAware{
protected Map<String, Object> session;
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
@Override
public void setApplication(Map<String, Object> application) {
// TODO Auto-generated method stub
}
@Override
public void setRequest(Map<String, Object> request) {
// TODO Auto-generated method stub
}
@Override
public void setServletContext(ServletContext context) {
// TODO Auto-generated method stub
}
@Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
}
@Override
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
}
}
BaseAction父类写好后,LoginAction在操作session的时候可以不需要implements SessionAware接口。而改为extends BaseAction的方式统一管理。
package cn.xdl.action;
public class LoginAction {
private String username;//<input type="text" name="username">
private String password;//<input type="password" name="password">
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
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 String execute() {
//判断账号密码是否正确
if ("scott".equalsIgnoreCase(username)
&&"1234".equalsIgnoreCase(password)) {
//采用继承父类方法注入的session
session.put("user", username);
return "success";
}else {
msg="账号或密码错误";
return "error";
}
}
}
request、session、application应用
Struts2框架对request、session、application都响应的做了Map封装,可以通过Map类型使用,也可以通过servlet类型使用。
1.ActionContext
获取Map类型的request、session、application,任何都可以使用
2.ServletActionContext
获取Servlet类型的request、session、application,任何都可以使用
3.Aware接口注入
获取Map类型或Servlet类型的request、session、application,只适用与Action组件