1.3 struts2相关(2.5.13)
struts2-core
struts2-spring-plugin
struts2与spring集成的插件
将action配置到spring中,struts的action标签的class属性填写spring中bean的名字或id
struts.xml配置注意事项:
1)
2)struts-Xxx.xml文件中的action的class属性类型填写spring中的id。
- web.xml配置
5.1 Spring上下文ApplicationContext.xml的加载
contextConfigLocation
classpath:applicationContext.xml
5.2 启动Web容器时,自动装配ApplicationContext.xml的配置信息
org.springframework.web.context.ContextLoaderListener
5.3 中文乱码过滤器
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
encodingFilter
/*
5.4 其他配置
5.4.1 防止内存泄露、缓存清除监听器
org.springframework.web.util.IntrospectorCleanupListener
5.4.2 用于支持3种Bean的作用域:request,session和globalSession
org.springframework.web.context.request.RequestContextListener
5.4.3 把session的关闭延迟到jsp页面显示之后,请配在struts2上面
OpenSessionInView
org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
OpenSessionInView
/*
5.5 Struts2核心过滤器
public class BookAction extends BaseAction implements ModelDriven<Book>{
private BookBiz bookBiz;
private Book book=new Book();
public BookBiz getBookBiz() {
return bookBiz;
}
public void setBookBiz(BookBiz bookBiz) {
this.bookBiz = bookBiz;
}
private static final long serialVersionUID = 6179830049309134413L;
public String toAdd() {
return "toAdd";
}
public String add() {
this.bookBiz.save(book);
return "add";
}
@Override
public Book getModel() {
// TODO Auto-generated method stub
return book;
}
}
strtus-base.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>
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.i18n.reload" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="base" extends="struts-default" abstract="true">
<global-allowed-methods>regex:.*</global-allowed-methods>
</package>
</struts>
struts-sy.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>
<package name="sy" extends="base" namespace="/sy">
<action name="bookAction_*" class="bookAction" method="{1}">
<result name="toAdd">/add.jsp</result>
<result name="add">/index.jsp</result>
</action>
</package>
</struts>
strtus.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>
<include file="struts-default.xml"></include>
<include file="struts-base.xml"></include>
<include file="struts-sy.xml"></include>
</struts>
xml:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Archetype Created Web Application</display-name>
<!-- 1、整合spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 2、整合struts2 -->
<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>*.action</url-pattern>
</filter-mapping>
<!-- 3、添加过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
index.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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/sy/bookAction_toAdd.action">新增</a>
</body>
</html>
add.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!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>add</h1>
<s:form namespace="/sy" action="bookAction_add">
<s:textfield label="书号" name="bid" />
<s:textfield label="书名" name="bname" />
<s:textfield label="价格" name="price" />
<s:submit value="确定"/>
</s:form>
</body>
</html>
如有不懂看前面SSH的文章