Simple SSH framework for simple landing

Source: Internet
Author: User

As I self-study, so learn from the following blog and the Wrong Modified blog:

http://blog.csdn.net/lylyyg/article/details/46932493

http://blog.csdn.net/zongzhankui/article/details/6099552

Http://wenku.baidu.com/link?url=lVKV4WNu1VjzoBksNh1fCOjDhlV3R25UBb90R4jgRjRkd5fRMbKClU9mHIHNwVxBR4UjE_ Ryrbzp1yt1vtxmjptj0p29jxd7qo1k04clphu

As a beginner javaweb, to understand the SSH framework is not easy, so only from the simple login to start, to better understand the SSH framework basic building and implementation of the functions of the steps.

Tools to prepare:

1.Myeclipse 10

2.struts2-spring-plugin-2.1.2.jar package (without adding this package, Strut.xml cannot access the ID of the class name injected into the Applicationcontext.xml bean)

3.tomcat 7.X or other version of Tomcat

4.Mysql 5.5 or above

5.navicat (visualization tool for MySQL database)

The first step:

Set up the database first:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/8B/6E/wKiom1hOL9bTrKcXAABrkYvCgoo876.png-wh_500x0-wm_3 -wmp_4-s_365301362.png "title=" [qb_x2tg~ocsbupl5ut9~o2.png "alt=" Wkiom1hol9btrkcxaabrkyvcgoo876.png-wh_50 "/>

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

Step Two:

Steps to build the SSH framework:

1. Create a Web project:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/8B/6E/wKiom1hOMJzwlSypAACVS5LT5Rk869.png-wh_500x0-wm_3 -wmp_4-s_1996327796.png "title=" u]tn~) 359wba0v0m8~{' {sv.png ' alt= ' wkiom1homjzwlsypaacvs5lt5rk869.png-wh_50 '/>

2. Add the SSH framework (right-click on the project name, and select Myeclipse->add strut2. [Other similar]):

The Order of additions is: strut2->spring->hibernate

Refer to Baidu can or the above-mentioned Baidu link;


Step Three:

To set up a login function implementation module:

1. Use Hibernate reverse engineering to create persistent classes and *.hbm.xml files first.

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/8B/6A/wKioL1hONRuTx45TAAFsAZE_5g4716.png-wh_500x0-wm_3 -wmp_4-s_1857954090.png "title=") a90n (FVO[UNP) od1@gpt}7.png "alt=" Wkiol1honrutx45taafsaze_5g4716.png-wh_50 "/>

2. Writing the Ialogindao interface class and the Logindaoimpl class implement DAO class

The Ialogindao.java code is as follows:

Package Com.loginssh.dao;public Interface Ialogindao {Boolean login (String username,string pwd);

The Logindaoimpl.java code is as follows:

Package Com.loginssh.dao.daoimpl;import Java.util.list;import Org.springframework.orm.hibernate3.support.hibernatedaosupport;import Com.loginssh.dao.ialogindao;import Com.loginssh.model.userinfo;public class Logindaoimpl extends Hibernatedaosupport implements Ialogindao {@ Suppresswarnings ("Unchecked") Public boolean login (string userName, string pwd) {System.out.println (username+ "DAO" + PWD); List<userinfo> list=this.gethibernatetemplate (). Find ("From UserInfo where username=? And userpwd=? ", Username,pwd); Boolean flag=false; SYSTEM.OUT.PRINTLN ("List length:" +list.size ()); if (List.size () >0) {flag=true;} return flag;}}

Write the Ialoginservice interface class and the Userserviceimpl class implement UserService interface class:

The Ialoginservice.java code is as follows:

Package Com.loginssh.service;public Interface Ialoginservice {Boolean login (String username,string pwd);

The Loginserviceimpl.java code is as follows:

Package Com.loginssh.service.serviceimpl;import Com.loginssh.dao.ialogindao;import Com.loginssh.service.ialoginservice;public class Loginserviceimpl implements Ialoginservice {private Ialogindao logindao;public void Setlogindao (Ialogindao logindao) {This.logindao=logindao;} Public Ialogindao Getlogindao () {return this.logindao;} public boolean login (string userName, string pwd) {System.out.println (username+ "service" +pwd); return Logindao.login ( UserName, PWD);}}

Write Loginaction:

The Loginaction.java code is as follows:


package com.loginssh.action;import com.loginssh.service.ialoginservice;import  com.opensymphony.xwork2.actionsupport;public class loginaction extends actionsupport  {private ialoginservice loginservice;private string username;private string pwd; Public ialoginservice getloginservice ()  {return loginservice;} Public void setloginservice (Ialoginservice loginservice)  {this.loginService =  Loginservice;} Public string getusername ()  {return username;} Public void setusername (String username)  {this.username = username;} Public string getpwd ()  {return pwd;} Public void setpwd (STRING&NBSP;PWD)  {this.pwd = pwd;} Public string login () {System.out.println (GetUserName () + "Surprise" +getpwd ()); boolean flag= Loginservice.login (This.getusername (), this.getpwd ()); SYSTEM.OUT.PRINTLN (flag), if (flag) {return  "Success";} else{return  "Input";}}}

Add the following code to the Applicationcontext.xml:

<!--inject DAO's session persistence object--><bean id= "Logind" class= "Com.loginSSH.dao.daoImpl.LoginDaoImpl" >< Property Name= "Sessionfactory" ref= "sessionfactory" ></property></bean><!--service injected DAO ID for the following Ref class for service implementation class Name= "DAO"--><bean id= "logins" class= "declared in the service implementation class Com.loginSSH.service.serviceImpl.LoginServiceImpl "><property name=" Logindao "ref=" Logind "></property ></bean><!--Action Inject service--><bean id= "Logina" class= "Com.loginSSH.action.LoginAction" scope= " Prototype "><property name=" Loginservice "ref=" logins "></property></bean>

and configure the implementation of the spring listener in Web. XML:

<!--Spring Configuration-<!--Spring Listener--<listener> <listener-class> Org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!--implementation Listener-- > <context-param> <param-name>contextConfigListener</param-name> <param-value>/web-inf /applicationcontext.xml</param-value> </context-param>

Configure the from force to the submitted username and password in strut.xml:

<package name= "Default" extends= "Struts-default" namespace= "/" ><action name= "Login" class= "Logina" method= " Login "><result name=" input ">/index.jsp</result><result name=" Success ">/main.jsp</result ></action></package>

To write a index.jsp page:

The index.jsp code is as follows:

<%@ page language= "java"  import= "java.util.*"  pageencoding= "Utf-8"%><%string  path = request.getcontextpath (); String basepath = request.getscheme ()  +  "://" + request.getservername ()  +   ":"  + request.getserverport () + path +  "/";%><! doctype html public  "-//w3c//dtd html 4.01 transitional//en" >

Create a main.jsp file under Webroot:

main.jsp code default can be;


The final step is to deploy the project to Tomcat:

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/8B/6A/wKioL1hOOXviCWaFAAAEp_EeUOg439.png-wh_500x0-wm_3 -wmp_4-s_2916043462.png "title=") 8ZR (G] (hpl]a263{{26djq.png "alt=" Wkiol1hooxvicwafaaaep_eeuog439.png-wh_50 "/>

Test login appears:


650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/8B/6A/wKioL1hOOcuB4qyBAAAKT4daO_8379.png-wh_500x0-wm_3 -wmp_4-s_1941484826.png "title=" Wg9s[a[3a]z$mp~l5{4wyb5.png "alt=" Wkiol1hoocub4qybaaakt4dao_8379.png-wh_50 "/>

Indicates success.

Precautions:

1. The ID name of the bean injected in applicationcontext.xml may not be found in Strut.xml, add Struts2-spring-plugin-2.1.2.jar to the Web-inf/lib folder.

 2. May appear java.lang.NoSuchMethodErr Or:antlr.collections.AST.getLine () I exception, the exception is specific The operation is as follows: Under MyEclipse,windows-->preferences--> Search in the text box struts2--> select Antlr2.7.2-->remove, If you have already run a project, you also want to delete the Tomcat directory Lib's antlr-2.7.2.jar.

This article only as the blogger's personal experience and records, if there is infringement, please contact the blogger to modify or delete. Thank you.

This article from the "10985165" blog, reproduced please contact the author!

Simple SSH framework for simple landing

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.