Spring MVC series: (0) struts2

Source: Internet
Author: User
Tags tojson



1, How to use Struts2?

Steps :

(1) Introduction of JAR Package

(2) configuration: Web. XML and Struts.xml configuration

(3) Write code and Configuration:

Custom Helloworldaction class (inherited from Actionsupport)

Registering the helloworldaction in Struts-web.xml


(1) Introduction of JAR Package

Reference: http://lsieun.blog.51cto.com/9210464/1791218

2.1. Introduction of JAR Package


(2) configuration: Web. XML and Struts.xml configuration

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>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/87/54/wKioL1fdjJ3SBSbXAACUefesGkc152.jpg "title=" 005. JPG "alt=" 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) Write code and Configuration :

Custom Helloworldaction class (inherited from 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 {@Overridepublic String execute () throws Exception {return action.success;}}

Registering the helloworldaction in Struts.xml

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& Lt;/result> </action> </package></struts>


2. How do I return json?

Steps:

(1) introduction of Struts2-json-plugin-2.3.29.jar

(2) <package> inherit from Json-default package;<result> The type value is Json.


(1) introduction of Struts2-json-plugin-2.3.29.jar

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/87/57/wKiom1fdiqzwSS3GAABTXzSHc6w108.jpg "title=" 001. JPG "alt=" wkiom1fdiqzwss3gaabtxzshc6w108.jpg "/>

(2) <package> inherit from Json-default package;<result> The type value is 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; @Overridepublic String execute () throws Exception {return action.success;} Public String test () throws exception{emp = new emp ("abc", "lucy", and 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>       &nBsp;</action>    </package></struts> 

650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M01/87/57/wKiom1fdi5GAK72dAABAbg9LdYg348.jpg "title=" 002. JPG "alt=" wkiom1fdi5gak72daababg9ldyg348.jpg "/>

3, how to deal with the date, Chinese (code)?

String date input: struts has a parameters interceptor, andThe parameters interceptor provides built-in type conversions that can convert strings to date types (see: http://lsieun.blog.51cto.com/ 9210464/1791975).

Date type output is string: when you output a date to a page, you can use the <s:date> tag of Struts. (reference: HTTPS://STRUTS.APACHE.ORG/DOCS/DATE.HTML)

<s:date name= "emp.birthday" format= "yyyy-mm-dd"/>


For input chinese, struts defaults to encoding the request without special handling, as long as it is a post request, Chinese does not appear garbled. (reference: http://blog.csdn.net/techbirds_bao/article/details/8233156)



4, how to integrate with spring?

(1) introduction of spring-core, Spring-web jar packages and struts and spring-integrated jar packages

(2) Configuring Web. XML and Applicationcontext.xml

(3) the object to be created is to be created by the spring IOC


(1) Introduction of JAR Package

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 and spring-integrated jar packages

Struts2-spring-plugin-2.3.29.jar



(2) Configuring Web. XML and Applicationcontext.xml

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  Configuration  -->  <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  Configuration  -->  <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) the object to be created is to be created by the 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 using HelloWorld registered in spring

<?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, two important configuration files

Two important documents under the Struts2-core-2.3.29.jar

/struts-default.xml(defines the Struts-default package and a series of Interceptors)

/org/apache/struts2/default.properties (defines The properties that need to be configured in Struts)

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/87/57/wKiom1fdjNuCHwfFAABadgBaBIU071.jpg "style=" float: none; "title=" struts-default.xml "alt=" wkiom1fdjnuchwffaabadgbabiu071.jpg "/>

650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M00/87/54/wKioL1fdjNvyFmQrAACIklJ0-I8520.jpg "style=" float: none; "title=" default.properties "alt=" wkiol1fdjnvyfmqraaciklj0-i8520.jpg "/>


6. Struts2 and Spring introduce other file methods

Struts2

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


Spring

<import resource= "com/rk/web/spring-web.xml"/>




Spring MVC series: (0) struts2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.