Create a Struts2 project to experience MVC

Source: Internet
Author: User

Step 1. Create a new project named struts2hello web project under MyElipse; 2. Import the Jar package to copy the jar package of struts 2 to the WEB-INF/lib/directory; or in the project. add [html] <classpathentry kind = "con" path = "melibrary.com. genuitec. eclipse. struts2.MYECLIPSE _ STRUTS21_CORE "/> and refresh the project. 3. Configure Filter to modify web. xml file, configure the core Filter of Struts 2; [html] <filter> <filter-name> struts2 </filter-name> <filter-class> org. apache. struts2.dispatcher. filterDispatcher </filter-class> </filter> <filter-mapping> <filter-name> struts2 </filter-name> <url-pattern> /*. action </url-pattern> </filter-mapping> 4. Configure struts. add struts in the src directory for xml. xml configuration file; [html] <? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.1 // EN "" http://struts.apache.org/dtds/struts-2.1.dtd "> <Struts> <include file =" struts-default.xml "/> <constant name =" struts. i18n. encoding "value =" UTF-8 "/> <package name =" default "extends =" struts-default "> <global-results> <result name =" login "> login. jsp </result> <result name = "index"> index. jsp </result> </global-results> <action name = "UserAction" class = "com. app. action. userAction "/> </package> </struts> 5. implement controller [java] package com. app. hello; import co M. opensymphony. xwork2.Action; // POJO (Plain Object Java Object low intrusive) public class UserWorld implements Action {// get the control on the page, just inject the corresponding attributes here, provides the setter and getter methods. (The name must be exactly the same) private User user; // action class each attribute corresponds to a control attribute in the page private UserDAO userDAO = new UserDAO (); // action default execution method public String execute () {if (! UserDAO. login (user. getUsername (), user. getPassword () {ActionContext ac = ActionContext. getContext (); ac. put ("msg", "Incorrect login information"); // request. setAttribute ("",) ;}else {return "index"; // forward to the page corresponding to the logical view} return "login" ;}public User getUser () {return user;} public void setUser (User user) {this. user = user ;}// http://localhost:8080/Struts2HelloWorld/hello!aliasAction.action Public String aliasAction () {name = ""; return "success" ;}} 6. Modify struts. add Action ing and logical view redirection to the xml file, and add the following content in <struts> </struts>; [html] <package name = "default" extends = "struts-default"> <action name = "hello" class = "com. app. hello. helloWorld "> <result name =" success ">/helloworld. jsp </result> </action> </package> 7. Add the JSP view layer named helloworld. jsp [html] <% @ page contentType = "text/html; charset = UTF-8" %> <% @ tagl Ib prefix = "s" uri = "/struts-tags" %> <% @ page isELIgnored = "false" %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">

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.