1、如何使用Struts2?
步骤:
(1)引入JAR包
(2)进行配置:web.xml和struts.xml配置
(3)写代码和配置:
自定义HelloWorldAction类(继承自ActionSupport)
在struts-web.xml中对HelloWorldAction进行注册
(1)引入JAR包
参考:http://lsieun.blog.51cto.com/9210464/1791218 2.1、引入jar包 |
(2)进行配置:web.xml和struts.xml配置
web.xml
<?xml version="1.0" encoding="UTF-8"?> <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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>springMVC-struts2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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>*.action</url-pattern> </filter-mapping> </web-app>
struts.xml
<?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> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <include file="com/rk/web/struts-web.xml"></include> </struts>
(3)写代码和配置:
自定义HelloWorldAction类(继承自ActionSupport)
HelloWorld.java
package com.rk.web; import java.util.Date; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionSupport; import com.rk.entity.Emp; import com.rk.entity.JsonResult; public class HelloWorldAction extends ActionSupport { @Override public String execute() throws Exception { return Action.SUCCESS; } }
在struts.xml中对HelloWorldAction进行注册
struts-web.xml
<?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="myweb" namespace="/" extends="struts-default"> <action name="hello_*" class="com.rk.web.HelloWorldAction" method="{1}"> <result name="success">/success.jsp</result> </action> </package> </struts>
2、如何返回JSON?
步骤:
(1)引入struts2-json-plugin-2.3.29.jar
(2)<package>继承自json-default包;<result>中type值为json。
(1)引入struts2-json-plugin-2.3.29.jar
(2)<package>继承自json-default包;<result>中type值为json。
HelloWorld.java
package com.rk.web; import java.util.Date; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionSupport; import com.rk.entity.Emp; public class HelloWorldAction extends ActionSupport { private Emp emp; @Override public String execute() throws Exception { return Action.SUCCESS; } public String test() throws Exception{ emp = new Emp("abc", "lucy", 23, new Date()); return "toJson"; } public Emp getEmp() { return emp; } }
struts-web.xml
<?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="myweb" namespace="/" extends="json-default"> <action name="hello_*" class="com.rk.web.HelloWorldAction" method="{1}"> <result name="success">/success.jsp</result> <result name="toJson" type="json"> <param name="root">emp</param> </result> </action> </package> </struts>
3、如何对日期、中文(编码)进行处理?
字符串日期输入:struts有一个Parameters拦截器,Parameters拦截器提供了内置的类型转换,可以将字符串转换为日期类型(参考:http://lsieun.blog.51cto.com/9210464/1791975 )。
Date类型输出为字符串:将日期输出到页面时,可以使用Struts的<s:date>标签。(参考: https://struts.apache.org/docs/date.html )
<s:date name="emp.birthDay" format="yyyy-MM-dd"/>
对于输入中文,struts默认对request进行了编码设置,而不需要特别处理,只要是POST请求,中文不会出现乱码。(参考 : http://blog.csdn.net/techbirds_bao/article/details/8233156 )
4、如何与Spring融合?
(1)引入spring-core、spring-web的jar包以及struts与spring整合的jar包
(2)配置web.xml和applicationContext.xml
(3)将需要创建的对象交由Spring的IOC来创建
(1)引入jar包
spring-core | commons-logging-1.2.jar spring-beans-3.2.5.RELEASE.jar spring-context-3.2.5.RELEASE.jar spring-core-3.2.5.RELEASE.jar spring-expression-3.2.5.RELEASE.jar |
spring-web | spring-web-3.2.5.RELEASE.jar |
struts与spring整合的jar包 | struts2-spring-plugin-2.3.29.jar |
(2)配置web.xml和applicationContext.xml
web.xml
<?xml version="1.0" encoding="UTF-8"?> <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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>springMVC-struts2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 1. struts2 配置 --> <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>*.action</url-pattern> </filter-mapping> <!-- 2. spring 配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <display-name>spring</display-name> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
applicationContext.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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="com/rk/web/spring-web.xml"/> </beans>
(3)将需要创建的对象交由Spring的IOC来创建
spring-web.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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="helloword" class="com.rk.web.HelloWorldAction" scope="prototype"></bean> </beans>
struts-web.xml中使用在spring中注册的helloworld
<?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="myweb" namespace="/" extends="json-default"> <action name="hello_*" class="helloword" method="{1}"> <result name="success">/success.jsp</result> <result name="toJson" type="json"> <param name="root">emp</param> </result> </action> </package> </struts>
5、两个重要配置文件
struts2-core-2.3.29.jar下的两个重要文件
/struts-default.xml (定义了struts-default包和一系列的拦截器)
/org/apache/struts2/default.properties (定义了struts中需要配置的属性)
6、struts2和spring引入其它文件方式
struts2
<include file="com/rk/web/struts-web.xml"></include>
spring
<import resource="com/rk/web/spring-web.xml"/>
原文地址:http://lsieun.blog.51cto.com/9210464/1853477