How to build a Struts database application with landing page

zhaozj2021-02-16  50

How to build a Struts database application with landing page

[Related Links]

"JavaWebstudio Series Development Tools Visaul Struts Version" http://dev.9cbs.net/develop/Article/28/28871.shtm

"How to achieve the simplest Struts program" http://dev.9cbs.net/develop/Article/28/28946.shtm

"The implementation of the Struts drop-down box" http://dev.9cbs.net/develop/Article/28/28956.shtm

"The implementation of the Sruts radio box" http://dev.9cbs.net/develop/Article/29/29042.shtm

"Struts check box" http://dev.9cbs.net/develop/Article/29/29043.shtm

"The relationship between the Struts programs (1)" http://dev.9cbs.net/develop/Article/29/29131.shtm

The relationship between the "Struts program each file (2)" http://dev.9cbs.net/develop/Article/29/29133.shtm

"How to implement Struts database application (1) http://dev.9cbs.net/develop/Article/29/29151.shtm

"How to implement Struts database applications (2)" http://dev.9cbs.net/develop/Article/29/29138.shtm

"How to implement pagination display in the Struts database application (1)" http://dev.9cbs.net/develop/Article/29/29139.shtm

"How to implement pagination display in the Struts database application (2)" http://dev.9cbs.net/develop/Article/29/29140.shtm

"How to implement records in the Struts database application" http://dev.9cbs.net/develop/Article/29/29219.SHTM

"How to implement records in the Struts database application (continued)" http://dev.9cbs.net/develop/Article/29/29222.shtm

"How to achieve records in the Struts database application" http://dev.9cbs.net/develop/Article/29/29224.shtm

"How to implement records in the Struts database application (continued)" http://dev.9cbs.net/develop/Article/29/29227.shtm

"How to build a Struts database application with landing page"

"How to build a Struts database application with landing page and role"

http://community.9cbs.net/expert/topic/3084/3084548.xml?temp=.9735529

http://dev.9cbs.net/user/kui

The above example development environment: Java Web Studio Series Development Environment Visual Strutst version.

The latest JavaWebStudio download address: ftp: //210.36.64.79/kui

The above example source program download address: ftp: //210.36.64.79/kui

[the goal]

The goal of this example is to implement a user login application. Save user information with SQL Server 2000 database. Implement the user login page, user login verification, login check tag and other functions.

Define a field named "Users" table:

Enter some test data:

[effect]

Welcome page:

Log in page: Login is completed, the main menu page:

If you do not log in but run the main menu page user query page, you will automatically go to the user login page, and no longer give a show.

[background knowledge]

[step]

This example is compared with the steps in the "How to Struts Database Application", in addition to a selection "Struts Database Application Template" with Login Page "when establishing a new project, another selection" empty struts database application template ", and The JSP page needs to be checked if the user has logged in to add a label "", the other steps are exactly the same, so the same steps details "How to implement Struts Database Applications".

1. Open the Visual Struts development environment:

Start JavaWebStudio.exe files from the desktop JavaWebstudio fast or bin directory launched the Visual Struts development environment of JavaWebStudio.

2. Create an "empty Struts database application template application" application:

Select the menu "File" - "New" - "New Project", pop up the new project dialog, as shown in Figure 1 below:

Select "Empty Struts Database Application Template" in the dialog box, enter "Logon", Package (Package ", and location (path path) in the project name, which is of course changed as needed. Finally, click the "OK" button to complete the establishment of the new project.

In the root of the Logon project, you contain four subdirectories and two files, which is the standard structure of the JavaWebStudio project, it is best not to change their default names, otherwise there will be problems.

With the "Struts Database Application Template" with Login Page, the logon.jsp and LogonAction.java files required for the user login are automatically generated, and also generates files for user login demo, where index.htm is with There is a "landing" button, click the "Login" button to go to the logon.jsp login input page, the login input is completed and submit, and if you log in, go to the MainMenu.jsp main menu page. Add a tab when the main menu page tag is added to check if the user has already logged in. If you have logged in, continue the page, otherwise you will automatically go to the login page.

Logon.jsp file:

INDEX.HTM file:

MainMenu.jsp file:

3. Use the Struts Database Application File Wizard to establish a new application:

To click the right mouse button in the JavaWebStudio file manager, select the Struts Database Application File Wizard to enter the Struts File Wizard dialog box, or select the menu "file" - "Struts Database Application File Wizard" The Struts File Wizard dialog box.

Enter the "Useredit" file name in the filename input field under the Struts file wizard dialog jsp file, other files use the default settings.

(1) Enter the file name:

(2) Import the field information of the SQL statement and the data table from the data source (field name, and field type):

(3) Select the desired table "User" from the data table.

It is important to note that the "Field information of the SQL statement and data table from the data source" and "Select the table from the data table" is merely generated to automatically generate SQL statements and read field information, here Complete can be manually entered directly, unrelated to the configuration of the database connection pool, below, and then configure the database connection pool separately.

Next, configure the database connection pool, and other property pages can be used by default, and the data connection configuration property page is directly turned directly. (4) JSPOUT property page settings:

The setting of the JSPOUT property page is the only step in the "How to Implement Struts Database Application", which is the core content of this example. As can be seen from the figure, we have to do it just to add the "Record Operation" selection in the settings of the "Record Operation" selection item.

(5) Configure the database connection pool:

Here you need to note that the user login is also used to use this database connection configuration.

(6) Plus the user login check label in the newly generated JSP file :

The newly generated JSP file has three, query conditions Enter file useerEdit.jsp, add new record input files UserEditInsert.jsp and query results display file useritout.jsp. Add the user login check tag after the tag of these three files, respectively.

4, project file structure

Project directory structure analysis See "How to achieve the simplest struts program", this Struts database application file wizard automatically generates six files, where the web directory is input, output two JSP files, the SRC directory is a Java file. All program source code is automatically generated by the file wizard except that the file name is automatically generated, and all configurations are also automatically completed.

Refresh the JavaWebStudio file manager, then the three files are automatically generated in the web directory, which is useeredit.jsp and useeredit.jsp and useritinsert.jsp, the former is an input JSP file, the middle is output JSP file, the latter is adding a record Page file; SRC / EMPTYPRJ directory automatically generates four files, which are useeredit action.java, useeredit form.java, the former is an action file, the latter is a formaction file; there is a database operation file Useredit.java and Usereditdao. Java, the former is used to save the bean recorded by the data table, and the latter is a database operation DAO file.

4.1, modify the DAO file:

If the data is selected, SQL Server 2000, you need to put the following code in the DAO file:

// Please join the down with SQL Server:

//con.setautoCommit (true);

// SQL Server

Both rewritten into the following code:

// Please join the down with SQL Server:

Con.SetAutocommit (True); // Note !! This is this line, it turned out to be annotated, now you have to get your annotation !!

// SQL Server

Through the above rewriting, the SQL Server 2000 can be recorded, deleted, and adding the SQL Server 2000 by JDBC.

4.2 Join the login identification label on the place where the user login identification is required

(1) Useredit.jsp file:

Plus label in

<% @ page contenttype = "text / html; charset = GB2312" Language = "java"%>

<% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%>

<% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "HTML"%> <% @ Taglib URI = "/ Web-inf / struts-logic.tld" prefix = "logic"%>

<% @ Taglib URI = "/ Web-INF / STRUTS-TEMPLATE.TLD" prefix = "template"%>

</ title></p> <p><HTML: BASE /></p> <p></ hEAD></p> <p><app: checklogon /></p> <p><body bgcolor = "white"></p> <p><html: form action = "/ usereditaction.do?action=search&search=search&pression=null" Method = "post"></p> <p>Please enter the username you want to find: <html: text property = "username" /></p> <p><html: Submit value = "Find" /></p> <p><html: reset value = "rewrite" /></p> <p></ html: form></p> <p><html: form action = "useereditaction.do?action=search&pression=null" Method = "post"></p> <p><html: submit value = "All Display" /></p> <p></ html: form></p> <p></ body></p> <p></ html: html></p> <p>4.3 Join a new menu item "User Check" in the left page of the MainMenu.jsp Main Menu page Left.htm:</p> <p>The added code is mainly: <a href="ustit.jsp" target=ma all users query </a></p> <p>5, compile, start the server, run:</p> <p>Compile the project and start the server by the buttons on the toolbar, then open index.htm and run index.htm through the "Run" button on the toolbar and enter the corresponding data:</p> <p>Click "Login":</p> <p>Enter the correct username and password, select "Send", go to the main menu page:</p> <p>If you do not log in but run the mainmenu.jsp main menu page or the UseRedit.jsp Query page, you will automatically go to the user login page, which is no longer demonstrated.</p> <p>Through the above steps, the estimated target function is achieved.</p> <p>[Program source code]</p> <p>Compared with "How to Implement Struts Database Applications", this example is mainly a logon.jsp, logonaction.java file that users need to log in, and custom label files used to check if the user has logged in Checklogontag.java, due to Logon The .jsp file does not have a special place, so it is not listed here.</p> <p>LogonAction.java file:</p> <p>Package EmptyPRJ;</p> <p>Import emptyprj.jdbc.util.ConnectionPool; import java.util.locale;</p> <p>Import javax.servlet.http.httpservletRequest;</p> <p>Import javax.servlet.http.httpsession;</p> <p>Import javax.servlet.http.httpservletResponse;</p> <p>Import org.apache.commons.logging.log;</p> <p>Import org.apache.commons.logging.logfactory;</p> <p>Import org.apache.struts.Action.action;</p> <p>Import org.apache.struts.Action.Actionerror;</p> <p>Import org.apache.struts.Action.Actionerro;</p> <p>Import org.apache.struts.Action.actionform;</p> <p>Import org.apache.struts.Action.actionForward;</p> <p>Import org.apache.struts.action.actionmapping;</p> <p>Import org.apache.struts.util.moduleException;</p> <p>Import org.apache.struts.util.MessageResources;</p> <p>Import org.apache.commons.beanutils.propertyUtils;</p> <p>Import java.sql.connection;</p> <p>Import java.sql.preparedStatement;</p> <p>Import java.sql.resultset;</p> <p>Import java.sql.sqlexception;</p> <p>Import java.util.hashmap;</p> <p>Import java.util.collection;</p> <p>Public Final Class LogonAction Extends Action {</p> <p>Private log log =</p> <p>Logfactory.getlog ("Org.apache.struts.WebApp.example");</p> <p>PRIVATE CONNECTIONPOOL POOL;</p> <p>Public logonAction () {</p> <p>Pool = connectionPool.getInstance ();</p> <p>}</p> <p>Public ActionForward Execute (ActionMapping Mapping,</p> <p>Actionform Form,</p> <p>HTTPSERVLETREQUEST REQUEST,</p> <p>Httpservletresponse response</p> <p>Throws exception {</p> <p>// Extract Attributes We Will Need</p> <p>Locale locale = getLocale (request);</p> <p>MessageResources Messages = GetResources (Request);</p> <p>// Validate the Request Parameters Specified by The User</p> <p>ActionerroS Errors = new actionerrors ();</p> <p>String UserName = (String)</p> <p>PropertyUtils.getsimpleProperty (Form, "UserName");</p> <p>String password = (string)</p> <p>PropertyUtils.getsimpleProperty (Form, "Password");</p> <p>IF (! Checkuser (username, password))</p> <p>{</p> <p>Errors.add (ActionerRors.global_ERROR, New Actionerror ("Error.Password.mismatch));</p> <p>}</p> <p>// Report Any Erroors We Have Discovered Back to the Original Form</p> <p>IF (! errors.isempty ()) {</p> <p>SaveerRors (Request, Errors);</p> <p>Mapping.getInputForward ());</p> <p>}</p> <p>// Save Our Logged-in User In the session</p> <p>HttpSession session = request.getations ();</p> <p>Session.SetaTRibute (constants.user_key, username);</p> <p>IF (log.Indebugeload ()) {</p> <p>Log.debug ("LogonAction: User '" UserName </p> <p>"'logged on in session" session.getID ());</p> <p>}</p> <p>// Remove the Obsolete form bean</p> <p>IF (mapping.getattribute ()! = null) {</p> <p>IF ("REQUEST" .Equals (mapping.getscope ()))))</p> <p>Request.Removettribute (mapping.getattribute ());</p> <p>Else</p> <p>Session.Removettribute (mapping.getattribute ());</p> <p>}</p> <p>// forward control to the specified surcess URI</p> <p>Mapping.Findforward ("Success"));</p> <p>}</p> <p>Public Boolean Checkuser (String Username, String Password)</p> <p>{</p> <p>Connection con = NULL;</p> <p>Try</p> <p>{</p> <p>Con = pool.getConnection ();</p> <p>String SQL = "SELECT * from DBo.Users where username =? And password =?";</p> <p>PreparedStatement PS = NULL;</p> <p>ResultSet RS = NULL;</p> <p>Try {</p> <p>IF (con?isclosed ()) {</p> <p>Throw new IllegalStateException ("Error.con.isClosed");</p> <p>}</p> <p>PS = con.preparestatement (SQL);</p> <p>ps.setstring (1, username);</p> <p>Ps.setstring (2, password);</p> <p>RS = ps.executeQuery ();</p> <p>IF (rs.next ())</p> <p>{</p> <p>Return True;</p> <p>}</p> <p>Return False;</p> <p>} catch (sqlexception e) {</p> <p>E.PrintStackTrace ();</p> <p>Throw new runtimeException ("Error.ps.executeQuery);</p> <p>} finally {</p> <p>Try {</p> <p>IF (PS! = NULL)</p> <p>ps.close ();</p> <p>IF (rs! = null)</p> <p>Rs.close ();</p> <p>} catch (sqlexception e) {</p> <p>E.PrintStackTrace ();</p> <p>Throw new runtimeException ("error.rs.close");}</p> <p>}</p> <p>}</p> <p>Catch (SQLException E)</p> <p>{</p> <p>E.PrintStackTrace ();</p> <p>Throw new runtimeException ("Unable to get connection.");</p> <p>}</p> <p>Finally</p> <p>{</p> <p>Try</p> <p>{</p> <p>IF (con! = null)</p> <p>C. close ();</p> <p>}</p> <p>Catch (SQLException E)</p> <p>{</p> <p>Throw new runtimeException (E.GetMessage ());</p> <p>}</p> <p>}</p> <p>}</p> <p>}</p> <p>Checklogontag.java file:</p> <p>Used to check if the user has logged in the custom tag original code.</p> <p>Package EmptyPRJ;</p> <p>Import java.io.ioException;</p> <p>Import javax.servlet.servletException;</p> <p>Import javax.servlet.http.httpsession;</p> <p>Import javax.servlet.jsp.jspexception;</p> <p>Import javax.servlet.jsp.tageXt.tagsupport;</p> <p>Import org.apache.struts.config.moduleconfig;</p> <p>Public final class checklogontag outs tagsupport {</p> <p>PRIVATE STRING NAME = constants.user_key;</p> <p>Private string Page = "/logon.jsp";</p> <p>Public string getname () {</p> <p>Return (this.name);</p> <p>}</p> <p>Public void setname (String name) {</p> <p>THIS.NAME = Name;</p> <p>}</p> <p>Public string getpage () {</p> <p>Return (this.page);</p> <p>}</p> <p>Public void setpage (string page) {</p> <p>THIS.PAGE = Page;</p> <p>}</p> <p>Public int desartTartTag () throws jspexception {</p> <p>Return (SKIP_BODY);</p> <p>}</p> <p>PUBLIC INT DOENDTAG () THROWS JSPEXCEPTION {</p> <p>// Is there a valid user logged on?</p> <p>Boolean Valid = False;</p> <p>HttpSession session = pageContext.getations ();</p> <p>IF ((session! = null) && (session.getattribute (name)! = NULL)) {</p> <p>VALID = TRUE;</p> <p>}</p> <p>// forward control based on the results</p> <p>IF (Valid) {</p> <p>Return (Eval_Page);</p> <p>} else {</p> <p>ModuleConfig CONFIG =</p> <p>(ModuleConfig) PageContext.getServletContext (). GetaTtribute (</p> <p>Org.apache.struts.globals.Module_Key);</p> <p>Try {</p> <p>PageContext.Forward (config.getprefix () page);</p> <p>} catch (servletexception e) {</p> <p>Throw new JSPEXCEPTION (E.TOString ());</p> <p>} catch (ioexception e) {</p> <p>Throw new JSPEXCEPTION (E.TOString ());</p> <p>Return (SKIP_PAGE);</p> <p>}</p> <p>}</p> <p>Public void release () {</p> <p>Super.release ();</p> <p>THIS.NAME = constants.user_key;</p> <p>THIS.PAGE = "/logon.jsp";</p> <p>}</p> <p>}</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-21386.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="21386" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.058</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'elsglsuiHgf8rMRFvCiRya7vBV8xmSJlXB8KAO3SptHTYKItGnIxCWi_2FmKKl5rBs_2FoEM_2BF5OhoHXBx_2Fm'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>