Struts Summary Case on the second day

Source: Internet
Author: User

The logon interface and the logon success jump interface are omitted here. Both interfaces are simple jsp pages. The bean file is omitted, but the username and password attributes are encapsulated. web. xml configuration file [java 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" 3 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 4 xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee 5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 6 <filter> 7 <filter-name> struts2 </filter-name> 8 <filter-class> org. apache. struts2.dispatcher. ng. filter. strutsPrepareAndExecuteFilter </filter-class> 9 </filter> 10 11 <Filter-mapping> 12 <filter-name> struts2 </filter-name> 13 <url-pattern>/* </url-pattern> 14 </filter-mapping> 15 16 <welcome-file-list> 17 <welcome-file> index. jsp </welcome-file> 18 </welcome-file-list> 19 </web-app> 20 2. struts. xml configuration file, the most important file for struts2 configuration. The name cannot be changed to 21 <? Xml version = "1.0" encoding = "UTF-8"?> 22 <! DOCTYPE struts PUBLIC 23 "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" 24 "http://struts.apache.org/dtds/struts-2.3.dtd"> 25 26 <struts> 27 <! -- 28 package: only the package and inheritance are provided. The name attribute can be a random name and a null value can be written, however, if not, Attribute "name" is required and must be specified for element type "package" is reported ". at; 29 namespace: Used to match the path written in jsp, that is, the path accessed in the browser; 30 extends: Inherit, inherit from other packages and have all the features of other packages, default inheritance struts-default package 31 --> 32 <package name = "test" namespace = "/csdn" extends = "struts-default"> 33 <! -- 34 the default class inherits the default class in the struts-default package <default-class-ref 35 class = "com. opensymphony. the attribute value of xwork2.ActionSupport "/> 36 class can be changed to a self-compiled class. You only need to enter the full limit name of the class. 37. If the permission name is incorrect, the Error filterStart occurs; 38 39 the default method is execute. 40 if you want to use other methods defined in your class, you must add the method attribute, if it is not added, the action in the result set will not be found. The error is the same as that in the name attribute of the result tag. If the method value is inconsistent with the method in the actual class, an error will occur: noSuchMethodException is not such a method 42 43 action class processing method through the method property to control which method to choose All action classes have a default processing method: execute () 44 45 --> 46 <action name = "demo" class = "www. csdn. struts2.demo. demoAction "method =" login "> 47 <! -- Receive com. opensymphony. in the xwork2.ActionSupport class, the execute method returns the string 48 and points to the returned string through the name attribute of the result tag. If the name attribute is not configured, the default value is name = "success" 49. If you use a self-compiled class, the name attribute here cannot be omitted to use the default value; otherwise, no result defined for action is returned. The default action 50 --> 51 <result name = "success">/SC is not used in the result set. jsp </result> 52 <result name = "login">/index. jsp </result> 53 </action> 54 </package> 55 </struts> 56 3. c3p0-config.xmlc 3p0 configuration file 57 <c3p0-config> 58 <Default-config> 59 <property name = "driverClass"> com. mysql. jdbc. driver </property> 60 <property name = "jdbcUrl"> jdbc: mysql: // localhost: 3306/struts2 </property> 61 <property name = "user"> root </property> 62 <property name = "password"> root </property> 63 <property name = "initialPoolSize"> 10 </property> 64 <property name = "maxIdleTime"> 30 </property> 65 <property name = "maxPoolSize"> 100 </property> 66 <property n Ame = "minPoolSize"> 10 </property> 67 <property name = "maxStatements"> 200 </property> 68 </default-config> 69 70 <named-config name = "mysql"> 71 <property name = "driverClass"> com. mysql. jdbc. driver </property> 72 <property name = "jdbcUrl"> jdbc: mysql: // localhost: 3306/struts2 </property> 73 <property name = "user"> root </property> 74 <property name = "password"> root </property> 75 <property name = "acquireIncrement"> 5 </Property> 76 <property name = "initialPoolSize"> 10 </property> 77 <property name = "minPoolSize"> 50 </property> 78 <property name = "maxPoolSize"> 1000 </property> 79 </named-config> 80 </c3p0-config> 81 4. dao implementation class 82 package www. csdn. dao; 83 84 import java. SQL. SQLException; 85 86 import org. apache. commons. dbutils. queryRunner; 87 import org. apache. commons. dbutils. handlers. beanHandler; 88 89 import www. csdn. Domain. user; 90 import com. csdn. util. DBManager; 91 92 public class ActionDao {93 94 public User login (String username, String password) throws SQLException {95 QueryRunner qr = new QueryRunner (DBManager. getDataSource (); 96 String SQL = "select * from login where username =? And password =? "; 97 Object [] params = {username, password}; 98 User user User = (User) qr. query (SQL, params, new BeanHandler (User. class); 99 return user; 100 101} 102 103} 104 5. action main class 105 package www. csdn. struts2.demo; 106 107 import java. SQL. SQLException; 108 109 import www. csdn. dao. actionda; 110 import www. csdn. domain. user; 111 112 import com. opensymphony. xwork2.ActionSupport; 113 114/*** 115*116 * Self-compiled act The ion class does not inherit ActionSupport, but does not inherit the class directly written by itself. 117 * but in most cases, it must inherit the ActionSupport class or implement the Action interface, because the ActionSupport class encapsulates most required constants, 118 * after inheritance, you can directly use these constants without the need to define the 119 */120 public class DemoAction extends ActionSupport {121 122 // encapsulate the request parameters, the username must be the same as the username of the input tag on the jsp page. 123 private String username; 124 private String password; 125 private User entity; 126 // The set method is used to obtain the parameter 127 public void setUsername (String u Sername) {128 this. username = username; 129} 130 public void setPassword (String password) {131 this. password = password; 132} 133 public User getEntity () {134 return entity; 135} 136 137 // The get method is used to obtain the value from the action and output 138/* public String getUsername () {139 return username; 140} */141 142 143 public String login () {144 ActionDao dao = new ActionDao (); 145 try {146 entity = dao. login (use Rname, password); 147} catch (SQLException e) {148 e. printStackTrace (); 149} 150 if (entity! = Null) {151 return SUCCESS; 152} else {153 return LOGIN; 154} 155} 156 public String execute () {157 System. out. println ("processing ...... "); 158 return SUCCESS; 159} 160} 161 6. util tool class 162 package com. csdn. util; 163 164 import java. SQL. connection; 165 import java. SQL. SQLException; 166 import javax. SQL. dataSource; 167 import com. mchange. v2.c3p0. comboPooledDataSource; 168 www.2cto. com169 public class DBManager {170 private static combooleddatasource ds = null; 171 static {172 // note that the name of the xml configuration file must be a c3p0-config.xml, you can refer to the help documentation, there is a name in the help. If the name is wrong, the driver cannot be loaded. The Null Pointer Error 173 ds = new ComboPooledDataSource ("mysql"); 174} 175 public static Connection getCon () throws SQLException {176 return ds. getConnection (); 177} 178 public static DataSource getDataSource () {179 return ds; 180} 181}

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.