Spring Integrated STRUTS2

Source: Internet
Author: User

In the previous Springdemo, spring and hibernate have been integrated together, and struts is now integrated into the Springdemo, which is a complete SSH-based simple project.

In the whole project construction process, I basically is from inside out--first import jar package, organize configuration file, finish the interface and class of persistence layer and control layer, finally build the JSP page of the presentation layer. The advantage is that each step can be measured, timely modification of the error, continue to the next layer of time no longer worry about the previous content.

1. Import the Strut2 jar package:

Commons-logging-1.0.4.jar----------Jakarta General-Purpose logging Package

Reemarker-2.3.8.jar--------------------Freemarker is a template engine, a generic tool for generating text output based on templates

Ognl-2.6.11.jar--------------------------Support OGNL expressions

Struts2-core-2.0.12.jar----------------struts2 Core Pack

Xwork-2.06.jar--------------------------Xwork Package because Struts2 is an extension of xwork, some classes are still associated with xwork classes

Commons-fileupload-1.2.1.jar-------struts upload and download

(Ognl is a powerful expression, through its simple and consistent expression syntax, you can access any property of the object, call the method of the object, traverse the whole object's structure diagram, realize the field type conversion and other functions. It uses the same expression to access the properties of the object. )

Add struts2-spring Integrated plug-ins: Struts2-spring-plugin-2.0.12.jar

The integration of the STRUTS2 with spring is made with the spring plug-in package Struts2-spring-plugin-x-x-x.jar is released with Struts2.

The spring plugin enhances the creation of core framework objects by overwriting (override) Struts2 objectfactory. When an object is created, it is associated with the id attribute in the spring configuration file with the class attribute in the Struts2 configuration file and, if found, by spring, otherwise created by the STRUTS2 framework itself and then assembled by spring.

If you do not use this plugin , you need to configure it in Struts.xml:
The XML code is as follows:

<constant name= "struts.objectfactory" value= "Spring"/>    <constant name= " Struts.objectFactory.spring.autoWire "value=" name "/>
Struts2-spring plug-ins specifically have the following functions:

1. Allow spring to create action, interceptror, and result.

2. Objects created by struts can be assembled by spring.

3. If spring objectfactory is not used, 2 interceptors are provided to automatically assemble the action.


2, in the Web. XML configuration struts2 used to the core filter.

<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>/*</url-pattern></ Filter-mapping>
3, in the SRC directory under the new Struts.xml file to configure.

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts public        "-//apache software foundation//dtd struts Configuration 2.0//en"        "/http Struts.apache.org/dtds/struts-2.0.dtd "><struts><constant name=" struts.objectfactory "value=" Spring "/ >    <!--struts2 action must be defined in a specified package space--    <package name= "Default" namespace= "/" extends= " Struts-default ">        <action name=" Login "class=" loginaction "method=" execute ">            <result name=" Success ">/WEB-INF/success.jsp</result>            <result name=" Login ">/web-inf/login.jsp</result >        </action>    </package></struts>
4, the establishment of loginaction, the use of @autowried automatic injection accountservice.

The Loginaction.java code is as follows:

Package Com.demo.action;import Org.springframework.beans.factory.annotation.autowired;import Com.demo.service.accountservice;import Com.opensymphony.xwork2.actionsupport;public class LoginAction extends Actionsupport {/** *  */private static final long serialversionuid = -1438866827399732426l; @Autowiredprivate Accountservice accountservice;private string username;private string password; @Overridepublic string execute () { SYSTEM.OUT.PRINTLN ("Run execute ..."), if (Accountservice.hasmatchaccount (username, password)) {return SUCCESS;} return LOGIN;} Public String GetUserName () {return username;} public void Setusername (String name) {this.username = name;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;}}
5, modify login.jsp, add success.jsp

Login.jsp content is as follows:

<%@ 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" >
Success.jsp content is as follows:

<%@ 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" >
6, so we completed the simple integration of STRUTS2, completed a simple SSH-based framework of the landing demo. There is a basic understanding of SSH and the next step is to learn more in depth.

Because I used spring when I was doing the project with my teacher, I felt good about it, so in the process of learning, we mainly took spring as the core to expand the learning of other frameworks.

Each frame has its rich features, and any article or book cannot be exhaustive. Need to be in the process of practice, while learning to use, gradually familiar.



Spring Integrated 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.