SSI Framework Learning Summary (MVC three-tier architecture) _JSP programming

Source: Internet
Author: User
Tags i18n stub xmlns hosting

I believe that the MVC three-tier architecture has been familiar with the gray, in this is not detailed, the individual feel SSI frame structure or more typical MVC three-tier architecture, or relatively easy to start. About the introduction of this piece I would like to particularly thank the next Frankhui children's shoes, with his help, I can quickly familiar with the structure, the framework of my learning SSI is still very helpful. The SSI framework is composed primarily of struts2,spring and Ibatis, who are responsible for the interaction and collaboration between the layers to achieve and integrate functionality across the Web. Struts is primarily responsible for data transfer and control, and spring relies on its powerful dependency injection technology to implement functions like bean hosting and consolidation, which is, of course, the tip of the iceberg in spring, and Ibatis as a lightweight or mapping framework, The realization of semi automatic object relational mapping is provided, and the degree of freedom is higher than hibernate.

Frame structure:

This is a map I found on the internet about the SSI framework, the personal feeling of the painting is pretty good, for beginners, the hierarchy is very clear, more practical (thank you for this big share):

It is obvious here that the general structure of the SSI framework as well as the interaction between the layers, the top-level presentation layer, in terms of Java is mainly jsp,html and other view-layer technology, which involves the familiar javascript,jquery and ExtJS. In the control layer, the main thing is to use the STRUST2 tag function to achieve action and view layer of data interaction, of course, can also use AJAX technology to achieve the same function, this is based on personal preferences. At the business logic layer. The main use of spring's dependency injection implementation of the business logic classes and instances of DAO class hosting, of course, all kinds of instances can be hosted in spring for Unified management and association, including transactions, data sources and so on. In the persistence layer, the implementation of the Ibatis Object Relational mapping provided by the developer can be used to write a specific SQL statement and implement the operation of the database through the corresponding XML configuration.

In short, the SSI framework can reduce the coupling of our code, enhance the robustness and reusability of the code, speed up the development speed, but there are some deficiencies, for example, due to the three kinds of framework of the configuration file more, but also brought us some inconvenience, especially for small applications is more so.

Related Demo Introduction:

Based on the previous study of SSI framework, I can not exception choose to do a user-managed web implementation, the project's general framework is as follows:

In general, according to the three layer of layered model to be divided, specifically not detailed, I believe we should be very understanding of the entire structure.

Ø Introduction to Development environment:

Tools: eclipse3.6+tomcat7+mysql5.1

Frame: struts2,spring3.0.5,ibatis2.3.4.726

Introduction to Development Steps and configuration:

The first step, the well-known must be imported Lib library, because I use the front-end is ExtJS, so also need to import ExtJS related JS and CSS dependent files. Because there are too many Lib libraries, there is no screenshot, only ExtJS dependent files are intercepted:

Such a basic environment is available, the design of the database needs to be based on the model layer of the object to determine.

The second step is to establish the relevant model layer, the code is as follows:

User.java:

 package com.broada.demo.entity;/** * * * @author SMM * * *     public class User {private int id;		User ID private String name;	User name private String password;	User password private String username;		User nickname private String address;
	Address public String GetUserName () {return username;
	} public void Setusername (String username) {this.username = username;
	Public String getaddress () {return address;
	public void setaddress (String address) {this.address = address;
	public int getId () {return id;
	The public void setId (int id) {this.id = ID;
	Public String GetName () {return name;
	public void SetName (String name) {this.name = name;
	Public String GetPassword () {return password;
	} public void SetPassword (String password) {this.password = password; }
 
}

The third step,strust related configuration, here is not detailed about the strust of the individual configuration and principle, directly above the relevant configuration file, mainly strust.xml and web.xml related configuration, some of the main configuration is as follows:

Xml:

<!--struts2 module-->
  <filter>
    <filter-name>struts2</filter-name>
    <!-- This is Struts2 's core filter-->
    <filter-class>
      org.apache.struts2.dispatcher.FilterDispatcher
    < /filter-class>
    
  </filter>
  <filter-mapping>
    <filter-name>struts2</ filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

Strust.xml:

<?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> <!--This is an important place, many people in the use of <s:include> child page, found that the sub-page garbled, how to change is not, the reason is in the Times, Struts2 default encoding for UTF-8, garbled comrade please see the code on your JSP page is not inconsistent with this. Just put it in line with the JSP code.--> <constant name= "struts.i18n.encoding" value= "UTF-8"/> <!--told Struts2 that I was going to use the Spring assembly plant , in fact, this is the default-_-!!! --> <constant name= "struts.objectfactory" value= "Spring"/> <!--struts2 extensions, such as Struts1, with the. Do, STRUTS2 defaults to. Action, which can be changed to something else, such as. dxd--> <constant name= "struts.action.extension" value= "action"/> <!--resource files  --> <constant name= "struts.custom.i18n.resources" value= "Messageresource" > </constant> <!-- User Registration Class--> <!--The abstract attribute shows that the action inherits from its own defined base action, and class uses the registeraction that is generated by spring--> <package Name= "register" extends= "Struts-default" > <action name= "Reg"Ister "class=" registeraction method= "AddUser" > <!--registered successfully--> <result name= "Success" >success.js
    P</result> <!--registration failure--> <result name= "input" >error.jsp</result> </action> <action name= "Login" class= "registeraction" method= "Loginuser" > <!--registered successfully--> <result name= "s    	
    Uccess ">success.jsp</result> <!--registration failure--> <result name=" error ">error.jsp</result> </action> </package> </struts>

So the strust configuration is almost complete.  

Step Fourth: Configure ibatis configuration files, mainly jdbc.properties,sqlmapconfig.xml , and user.xml configuration . Jdbc.properties is primarily used to configure the database's data source parameters and is initialized automatically when spring is loaded, and the configuration of theibatis data source can be hosted to Spring initialization , so this is not a detailed talk. Sqlmapconfig.xml is mainly to configure the location of the ibatis configuration file,user.xml is used to write the relevant database statements, the configuration is roughly as follows:

Jdbc.properties:

Jdbc.driverclass=com.mysql.jdbc.driver
jdbc.url=jdbc:mysql://localhost:3306/userinfo
jdbc.user=root
jdbc.password=123456
jdbc.minpoolsize=5
jdbc.maxpoolsize=20
jdbc.maxidletime=1800
Jdbc.acquireincrement=5
jdbc.maxstatements=50
jdbc.initialpoolsize=10
jdbc.idleconnectiontestperiod=1800
jdbc.acquireretryattempts=30

The detailed parameter implications are mentioned in the spring configuration file and are not elaborated.

sqlmapconfig.xml:

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE sqlmapconfig Public "-//ibatis.apache.org//dtd SQL Map Config 2.0//en"
"http://ibatis.apache.org/dtd/ Sql-map-config-2.dtd ">
<sqlMapConfig>
<!--user Information table-->
<sqlmap resource=" com/broada/ Demo/dao/ibaties/map/user.xml "/>
</sqlMapConfig>

User.xml:

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE sqlmap Public "-//ibatis.apache.org//dtd SQL Map 2.0//en"   
"HTTP://IBATIS.APACHE.ORG/DTD/SQL-MAP-2.DTD" >
<sqlMap>
  <typealias alias= "User" type= "Com.broada.demo.entity.User"/>
  <!--save registration information -->
  <insert id= "Insertuser" parameterclass= "user" >
    insert INTO
    user (Name,password,username, Address) VALUES (#name #, #password #, #username #, #address #)
  </insert>
  
  <select id= "Selsectuser" parameterclass= "java.lang.String" resultclass= "user" >
  	select * from user
  	where name = #name #;
  </select>
</sqlMap>

In this way, the Ibatis configuration is almost complete.

Step Fifth: Configure the related configuration files for spring, mainly to consolidate the ibatis and strust used in the bean, you need to configure Web.xml and applicationcontext-web.xml two profiles:

Xml:

<listener>
    <!--This is the future use of webapplicationutilcontent-->
    <listener-class>
			Org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  <!--springframework config files-->
  <context-param>
    <param-name>contextconfiglocation </param-name>
    <!--put the spring configuration file into the Springframework package under/web-inf/to facilitate unified management, A named rule is an XML file that starts with applicationcontent-, and automatically searches for all compliant profiles-->
    <param-value>/web-inf/spring/when initialized
      . Applicationcontext-*.xml
    </param-value>
</context-param>

Applicationcontext-web.xml:

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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-2.0.xsd "> <!--Configure the data source, the connection pool is c3p0, The specific parameters of the representative refer to C3p0 's own doc, very detailed.
    --> <bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method= "Close" > <property name= "Driverclass" value= "${jdbc.driverclass}"/> <property name= "Jdbcurl" value= "${jdbc.url}"/&G
    T <property name= "user" value= "${jdbc.user}"/> <property name= "password" value= "${jdbc.password}"/> T;property name= "minpoolsize" value= "${jdbc.minpoolsize}"/> <property name= "Maxpoolsize" value= Jdbc.maxpoolsize} "/> <property name= maxidletime" value= "${jdbc.maxidletime}"/> <property name= "Acqu Ireincrement "Value=" ${jdbc.acquirEincrement} "/> <property name= maxstatements" value= "${jdbc.maxstatements}"/> <property name= "Initia Lpoolsize "value=" ${jdbc.initialpoolsize} "/> <property name=" idleconnectiontestperiod "value=" ${jdb  C.idleconnectiontestperiod} "/> <property name= acquireretryattempts" value= "${jdbc.acquireretryattempts}" 
  /> </bean> <!--The value of the above data source is expressed in the expression, which is where the configuration file is placed in the Ibatis directory, which is jdbc.properties, setting the C3P0 parameters-->
    <bean id= "Propertyconfig" class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
  <property name= "Location" > <value>/WEB-INF/ibatis/jdbc.properties</value> </property> </bean> <!--Configure Ibatis Sqlmapclient, which is of course given to spring to deal with, where sqlmapconfig files are placed in the Web-inf Ibatis directory is also easy to manage- > <bean id= "sqlmapclient" class= "Org.springframework.orm.ibatis.SqlMapClientFactoryBean" > <property
   Name= "Configlocation" >   <value>/WEB-INF/ibatis/SqlMapConfig.xml</value> </property> <!--the data source used here is the data source configured above--> <property name= "DataSource" > <ref bean= "dataSource"/> </property> </bean> & Lt;bean id= "userdaoid" class= "Com.broada.demo.daoImpl.UserDaoImpl" > <property name= "sqlmapclient" Sqlmapclient "></property> </bean> <bean id=" Userdaoserviceid "class=" Com.broada.demo.serviceImpl .  Userdaoserviceimpl "> <property name=" Userdao "ref=" userdaoid "> </property> </bean> <!-- User Registration action--> <bean id= "registeraction" name= "registeraction" class= "Com.broada.demo.action.RegisterAction" Scope= "Prototype" > <property name= "userdaoserviceinter" ref= "Userdaoserviceid" ></property> </be An> </beans>

Thus, the approximate configuration of the SSI framework is complete.  

Finally write the relevant DAO layer, service layer, action layer and JSP, etc., I do not elaborate, directly related to the code:

Registeraction.java:

Package com.broada.demo.action;
/** * @author SMM * * Import Com.broada.demo.entity.User;
Import Com.broada.demo.service.UserDaoServiceInter;
 
Import Com.opensymphony.xwork2.ActionSupport;
	
public class Registeraction extends Actionsupport {private static final long serialversionuid = 1L;	
 
	Private Userdaoserviceinter Userdaoserviceinter;
	Public Userdaoserviceinter Getuserdaoserviceinter () {return userdaoserviceinter; } public void Setuserdaoserviceinter (Userdaoserviceinter userdaoserviceinter) {this.userdaoserviceinter = UserdaoSer
	Viceinter;  private String name;	User name private String password;	Password Private String username;		Nickname Private String address;
	Address public String GetUserName () {return username;
	} public void Setusername (String username) {this.username = username;
	Public String getaddress () {return address;
	public void setaddress (String address) {this.address = address; Public String GetName () {return name;
	public void SetName (String name) {this.name = name;
	Public String GetPassword () {return password;
	} public void SetPassword (String password) {this.password = password; Public String AddUser () {System.out.println ("Add success!")
		");
		
		User user = new user ();
		String name = THIS.name;
		String password = This.password;
		String username = this.username;
		
		String address = this.address;
		User.setname (name);
		User.setpassword (password);
		User.setusername (username);
		
		User.setaddress (address);
		
		Boolean B = userdaoserviceinter.insertuser (user);
		if (b==true) {return SUCCESS;
	else return INPUT;
		
		Public String Loginuser () {System.out.println ("login =======");
		String name = THIS.name;
		
		String password = This.password;
		
		User user = Userdaoserviceinter.querybyname (name);
		if (user!= null && password.equals (User.getpassword ())) {return SUCCESS; 
	else return ERROR; }
}

Userdao.java:

Package Com.broada.demo.dao;
 
/**
 * @author SMM * *
 
import java.util.List;
 
Import Com.broada.demo.entity.User;
 
Public interface Userdao {
	
	/**
	 * User Registration
	 * @param user
	 * @return
	
	/public boolean insertuser ( User user);
	
	/**
	 * Obtain user information by user name *
	 @param name
	 * @return
	 /Public user
	
	Querybyname (String name);	

Userdaoimpl.java:

Package Com.broada.demo.daoImpl;
/**
 * @author SMM * *
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
 
Import Com.broada.demo.dao.UserDao;
Import Com.broada.demo.entity.User;
 
public class Userdaoimpl extends Sqlmapclientdaosupport implements userdao{
 
 
 
	@Override public
	Boolean Insertuser (user user) {
		try {
			getsqlmapclienttemplate (). Insert ("Insertuser", User);
			return true;
		} catch (Exception e) {
			e.printstacktrace ();
			return false;
		}	
	}
 
	@Override public
	User querybyname (String name) {
		//TODO auto-generated method stub
		try {
			User user = (User) Getsqlmapclienttemplate (). queryForObject ("Selsectuser", name);
			return user;
		} catch (Exception e) {
			e.printstacktrace ();
			return null;}}}
	

Userdaoserviceinter.java:

Package com.broada.demo.service;
 
/**
 * @author SMM * *
 
import com.broada.demo.entity.User;
 
Public interface Userdaoserviceinter {
	
	/**
	 * User Registration Service Interface
	 * @param user
	 * @return
	
	/Public Boolean insertuser (user user);
	
	/**
	 * Get user Information interface
	 * @param name
	 * @return/Public user
	
	querybyname (String name)
based on username;

Userdaoserviceimpl.java:

Package Com.broada.demo.serviceImpl;
 
/**
 * @author SMM * *
 
import Com.broada.demo.dao.UserDao;
Import Com.broada.demo.entity.User;
Import Com.broada.demo.service.UserDaoServiceInter;
 
public class Userdaoserviceimpl implements Userdaoserviceinter {
 
	private Userdao Userdao;
	Public Userdao Getuserdao () {return
		Userdao;
	}
	public void Setuserdao (Userdao userdao) {
		This.userdao = Userdao;
	}
	@Override Public
	boolean insertuser (user user) {return
		userdao.insertuser (user);
	}
	@Override public
	User querybyname (String name) {
		//TODO auto-generated a stub
		return Userdao.querybyname (name);
	}
 

index.jsp:

<%@ page language= "java" contenttype= "text/html; Charset=utf8 "pageencoding=" UTF8 "%> <%@ taglib prefix=" s "uri="/struts-tags "%> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >  

The general step is this, personally feel that the SSI framework is pretty good, at least customization and freedom are better.

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.