Spring MVC系列:(0)struts2



一、如何使用Struts2?html

步驟java

(1)引入JAR包web

(2)進行配置:web.xml和struts.xml配置spring

(3)寫代碼和配置:express

    自定義HelloWorldAction類(繼承自ActionSupport)apache

    在struts-web.xml中對HelloWorldAction進行註冊json


wKiom1feuxvRVnq_AAHggWeQmcQ359.png


(1)引入JAR包app

參考:http://lsieun.blog.51cto.com/9210464/1791218 jsp

2.一、引入jar包ide


(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>

wKioL1fdjJ3SBSbXAACUefesGkc152.jpg

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>


二、如何返回JSON?

步驟:

(1)引入struts2-json-plugin-2.3.29.jar

(2)<package>繼承自json-default包;<result>中type值爲json。

wKioL1fevmfj3dHSAAFIVGW0KeI951.png



(1)引入struts2-json-plugin-2.3.29.jar

wKiom1fdiqzwSS3GAABTXzSHc6w108.jpg

(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>

wKiom1fdi5GAK72dAABAbg9LdYg348.jpg

三、如何對日期、中文(編碼)進行處理?

字符串日期輸入: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  )



四、如何與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>



五、兩個重要配置文件

struts2-core-2.3.29.jar下的兩個重要文件

/struts-default.xml(定義了struts-default包和一系列的攔截器)

/org/apache/struts2/default.properties (定義了struts中須要配置的屬性)

wKiom1fdjNuCHwfFAABadgBaBIU071.jpg

wKioL1fdjNvyFmQrAACIklJ0-I8520.jpg


六、struts2和spring引入其它文件方式

struts2

<include file="com/rk/web/struts-web.xml"></include>


spring

<import resource="com/rk/web/spring-web.xml"/>
相關文章
相關標籤/搜索