Basic Principles of Struts1.x and implementation of the registration module, basic principles of struts1.x

Source: Internet
Author: User

Basic Principles of Struts1.x and implementation of the registration module, basic principles of struts1.x

1. Write JavaBean: User, which must inherit from the ActionForm class

1 package myuser; 2 3 import org. apache. struts. action. actionForm; 4 5 public class User extends ActionForm {6 7 // without this eclipse warning 8 private static final long serialVersionUID = 5599822869891501922L; 9 10 private Integer id; 11 12 private String userName; 13 14 private String pwd; 15 16 public Integer getId () {17 return id; 18} 19 20 public void setId (Integer id) {21 this. id = id; 22} 23 24 public String getUserName () {25 return userName; 26} 27 28 public void setUserName (String userName) {29 this. userName = userName; 30} 31 32 public String getPwd () {33 return pwd; 34} 35 36 public void setPwd (String pwd) {37 this. pwd = pwd; 38} 39}


2. register the User's object user in the struts-config.xml

FormBean concept, as the name suggests, Form bean, Form data, in Struts1.x is received by the specialized ActionForm class, configured in the struts-config.xml

1      <form-beans>2          <form-bean name="user" type="myuser.User"/>3      </form-beans>

Some Code that has been written over and over again without any technical content is implemented by a framework, such:
User user = new User ();
GetParameter.
The role of FormBean to provide data for actions.
3. Compile the UserDao class method addUser (User user User)

1 public void addUser (User user) {2 String SQL = "insert into users (userName, pwd) values (?,?) "; 3 // get Connection 4 Connection conn = DBLib. getConn (); 5 // create database operation object 6 PreparedStatement pstmt; 7 try {8 pstmt = conn. prepareStatement (SQL); 9 pstmt. setString (1, user. getUserName (); 10 pstmt. setString (2, user. getPwd (); 11 // perform database operations 12 pstmt.exe cute (); 13 pstmt. close (); 14} catch (SQLException e) {15 e. printStackTrace (); 16} finally {17 try {18 // close the database connection 19 conn. close (); 20} catch (SQLException e) {21 e. printStackTrace (); 22} 23} 24 25}


4. Write AddUserAction, call the addUser method in the UserDAO class, and add users. This class must inherit from Action.

1 package action; 2 3 import javax. servlet. http. httpServletRequest; 4 import javax. servlet. http. httpServletResponse; 5 6 import org. apache. struts. action. action; 7 import org. apache. struts. action. actionForm; 8 import org. apache. struts. action. actionForward; 9 import org. apache. struts. action. actionMapping; 10 11 import dao. userDAO; 12 import myuser. user; 13 14 public class AddUserAction extends Action {15 16 public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, 17 HttpServletResponse response) throws Exception {18 User user = (User) form; 19 UserDAO dao = new UserDAO (); 20 dao. addUser (user); 21 return mapping. findForward ("list"); // jump to userlist. jsp page 22} 23 24}


5. Configure AddUserAction, path, name, scope, and forward. After successful addition, go to userlist. jsp.

 1 <?xml version="1.0" encoding="UTF-8"?> 2  3  <!DOCTYPE struts-config PUBLIC 4      "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 5      "http://struts.apache.org/dtds/struts-config_1_3.dtd"> 6  <struts-config> 7      <form-beans> 8          <form-bean name="user" type="myuser.User"/> 9      </form-beans>10      11      <action-mappings>12          <action path="/addUser" type="action.AddUserAction" name="user">13              <forward name="list" path="/userlist.jsp" />14          </action>15      </action-mappings>16  </struts-config>

Configuration instructions:

Form-beans: used to place multiple formbeans

From-bean: A single formbean

| Name: from-bean name, used for reference of the following action tag

| Type: the corresponding class, which must inherit the ActionForm

Action-mappings: used to place multiple actions

Action: A single action

| Path: the path name filtered by *. do (generally, this depends on the web. xml url-pattern configuration). "/" must be added "/"

| Type: corresponding class, which must inherit Action (or its subclass, which will be shown in the following example)

| Name: form-bean in form-beans

| Scope: the valid range of the formbean. two values: request and session.

| Attribute: The name of formbean in the valid range. If this attribute is not set, the default value is name. Generally, this attribute is not required.

Forward: the page to jump to after the action is processed.

| Name: used for mapping. findForward (name) in action

| Path: Page path. You must add "/" before "/"

5. JSP page code

1 <% @ page language = "java" contentType = "text/html; charset = UTF-8" 2 pageEncoding = "UTF-8" %> 3 <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" http://www.w3.org/TR/html4/loose.dtd "> 4 


Code link: http://pan.baidu.com/s/1qY4PEOW extract code: gy0n

Related Article

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.