Struts2,hibernate,spring Integrated Notes (4) integration of--struts and spring

Source: Internet
Author: User

To eat a mouthful of rice, the procedure must be written one step at a

A lot of things that seem complicated are very simple at first.

Here's how to integrate struts and spring

Spring is our housekeeper, so we just throw it all over the trouble.

Write a test method first

 Packagecom.hibernate;Import Staticorg.junit.assert.*;Importorg.hibernate.SessionFactory;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;Importcom.beans.HelloWorldAction; Public classSpringtest {PrivateApplicationContext ac=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); @Test Public voidTest () {Helloworldaction a= (helloworldaction) ac.getbean ("Helloworldaction");    System.out.println (a); }}

Of course, it's definitely not going to be true.

Spring doesn't know what our helloworldaction is.

We must first declare

In Helloworldaction "public class Helloworldaction"

Added above

@Controller

@Scope ("prototype")

Now the test above is ready to run,

But it's just that spring takes over the Helloworldaction class and doesn't mean that spring and struts integrate

It's a spring HelloWorld.

Next, we need to add a listener to the Web. xml

New 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" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id= "Webapp_ ID "version=" 3.0 "> <display-name>oa</display-name> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> < param-value>classpath:applicationcontext*.xml</param-value> </context-param> <filter> &lt ;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-pat Tern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</ Welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</ Welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</ Welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list></web-app> 

And then join in a Struts2-spring-plugin-2.1.8.1.jar

Change the class property of the action in Struts.xml to "Helloworldaction"

Conventional, first letter lowercase, other unchanged, write wrong to error

Restart the server and enter it in the browser

Localhost:8080/oa/test/helloworld.action

No, the error will succeed.

Struts2,hibernate,spring Integrated Notes (4) integration of--struts and spring

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.