The MyEclipse plugin quickly generates an SSH project and configures a hyper-detailed procedure for injecting service into the action layer

Source: Internet
Author: User

Recently discovered that I am still unfamiliar with the automatic injection configuration of SSH, so I tidied up and finally made a simple injection configuration. It used to be in Applicationcontext.xml.

class= "Com.dj.ssh.action.LoginAction" scope= "prototype" autowire= "ByName" >    <property name= " UserService "ref=" UserService "></property>    class=" Com.dj.ssh.service.impl.UserServiceImpl ">    <property name=" Userdao "ref=" Userdao "></property>    </bean>     class=" Com.dj.ssh.dao.impl.UserDaoImpl ">    <property name=" sessionfactory "ref=" Mysessionfactory "></ Property>    </bean

Later found that mdzz, directly injected into the service, injected on it can be.

So there is the simplest configuration of this SSH framework.

Novice can take a look. I'll start with the first step of building an SSH framework with myeclipse, and I've talked about injecting service

Environment: MyEclipse 8.5, with Eclipse plug-in how to quickly build SSH project, or self-Baidu bar (in fact, most bloggers this time will say: Please refer to my other blog)

OK, new Web project, project name demo

Add struts support right click on your project Select myeclipse--"Add struts capabilities then you will see this interface at this point you need to click on the interface at the end of the struts2.1 click to finish it. Struts Support Configuration Complete , note that at this point, first configure spring support, you must first configure spring support at this point in fact, the above several tick not hooked so much can also be at this time. If you have configured the database, you do not need to match. If no child shoes have been configured click window--"Open perspective--" myeclipse java Persistence in the Open db Browser box, right click New in the box that opens, select MySQL connector and then Click Add JARS Select a Mysql-connector-java.jar then fill in the Blue section click Finish to configure the third step to add hibernate support the same as the usual in the myeclipse to choose Add Sprin G then click Next click Next to proceed to the next step here to select the previously configured database then click Next click Finish, at this time a most elementary SSH project is automatically generated by you through the MyEclipse plugin. Next, to configure, inject service at the action layer first configure the Web. xml
<!--Configure the location of the spring configuration file--><context-param><param-name>contextconfiglocation</param-name> <param-value>classpath:applicationContext.xml</param-value></context-param><!--Configuration Spring Listener  --><listener><listener-class>         Org.springframework.web.context.ContextLoaderListener       </listener-class></listener>

Then configure Applicationcontext.xml

At this point, because we are automatically generated, so in the SRC directory in the Applicationcontext.xml file already have a lot of code. We don't have to worry about him.

Just add three lines of key code to the beginning or the end.

<!--turn on automatic assembly    -<context:annotation-config></context:annotation-config><!--Set the scan path configuration later, He will automatically scan all packages under the Com.service.*.* path- -  <context:component-scan base-package = "com" use-  Default-filters= "false" >          <context:include-filter type= "regex" expression= "Com.service.*.*"/>      </context:component-scan>

Then configure the Struts.xml file

  <Packageextends= "Struts-default" >      class= "Com.action.UserAction" >     <result name= "Success" >index.html</result>    </action>    </ Package >

At this point, when I enter http://localhost:8080/demo/login.action in the browser, he forwards it through struts to the Com.action.UserAction file.

Useraction return to success, jump to index.html page

Com.action.UserAction Code

 Public classUseractionextendsactionsupport{PrivateUserService Service;//autowired automatically assemble service via set method@Autowired Public voidSetservice (UserService userservice) { This. Service =UserService;} @Override PublicString Execute ()throwsException {service.login (); if(service!=NULL)return"Success" ; Else return"False"; }}

In fact, there is no service layer built yet. Not to mention the Service.login () method.

So create a new Com.service package under the Com.service package to create a new UserService interface

UserService Interface Code

 Public Interface userservice {    void  login ();}

New Com.service.impl Package New Com.service.impl.UserServiceImpl file Implementation UserService interface

The code is as follows

 Public class Implements UserService {    publicvoid  login () {    System.out.println (" This is a landing method ");}    }

It's over now. The project structure diagram is as follows

Deploy the project under Tomcat. Run servers

How can it be 404? That's because I didn't write index.html.

In the Struts.xml configuration I configured to return success words to index.html page

And that's a judgment.

if (Service!=null) return "success";

So, the service injection was successful at this point.

Looking at the console.

Prefect Perfect, this is the SSH project plug-in generation, as well as configuration annotation injection service of the entire process

The MyEclipse plugin quickly generates an SSH project and configures a hyper-detailed procedure for injecting service into the action layer

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.