Struts2 entry Demo and struts2demo

Source: Internet
Author: User

Struts2 entry Demo and struts2demo

This article describes the struts2 getting started Demo. Share it with you for your reference. The details are as follows:

1. Create a Web Project named struts2Demo;

2. Create a user library struts2, which contains at least six jar files of struts2;
In fact, it is not necessary for MyEclipse8 or above, because it directly supports struts2 and does not need to export packages.

3. Use Build Path to add the struts2 database;

4. Add the following configuration to web. xml:

<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <! -- Struts2 configuration --> <filter-name> struts2 </filter-name> <filter-class> org. apache. struts2.dispatcher. ng. filter. strutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name> struts2 </filter-name> <url-pattern>/* </url-pattern> </filter-mapping> <welcome-file-list> <welcome-file> index. jsp </welcome-file> </welcome-file-list> </web-app>

5. Create an Action

Package com. yenange. action; import com. opensymphony. xwork2.Action; public class LoginAction implements Action {private String uname; // name private String upass in the form; // password in the form // execution method @ Override public String execute () throws Exception {System. out. println ("name:" + uname); System. out. println ("Password:" + upass); if (uname. equals ("leaf") & upass. equals ("leaf") {return "success";} return "error";} // remember to have the get and set Methods public String getUpass () {return upass ;} public void setUpass (String upass) {this. upass = upass;} public String getUname () {return uname;} public void setUname (String uname) {this. uname = uname ;}}

6. Three page files:

Index. jsp:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" gbk "%> 

Welcome. jsp:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> 

Fail. jsp:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" gbk "%> 

7. Configure struts. xml. (For MyEclipse6, you can only copy the file from other places)

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   "http://struts.apache.org/dtds/struts-2.0.dtd" > <struts>   <package name="default" namespace="/" extends="struts-default">     <action name="login" class="com.yenange.action.LoginAction">       <result name="success">/welcome.jsp</result>       <result name="error">/fail.jsp</result>     </action>   </package>   <!-- Add packages here --> </struts>

8. Now, you can run it. Is it easy?

The following describes how to obtain the request, session, and application.

Package com. yenange. action; import java. util. map; import javax. servlet. servletContext; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import javax. servlet. http. httpSession; import org. apache. struts2.ServletActionContext; import org. apache. struts2.interceptor. servletRequestAware; import org. apache. struts2.interceptor. sessionAware; import org. apache. struts 2. util. servletContextAware; import com. opensymphony. xwork2.Action; public class LoginAction implements Action, ServletRequestAware, SessionAware, ServletContextAware {private String uname; // The name private String upass in the form; // The password HttpServletRequest request in the form; // common request Map sessions; // The common sessions are different, but ServletContext application is not required; // common application // The second method is used; httpServletRequest request2 = ServletAc TionContext. getRequest (); HttpServletResponse response2 = ServletActionContext. getResponse (); HttpSession session2 = request2.getSession (); @ Override public void setServletRequest (HttpServletRequest req) {this. request = req ;}@ Override public void setSession (Map session) {this. session = session;} @ Override public void setServletContext (ServletContext application) {this. application = application ;} // Execution method public String execute () throws Exception {if (uname. equals ("leaf") & upass. equals ("leaf") {request. setAttribute ("userName", uname); return "success";} request. setAttribute ("accessError", uname + "this user or password is incorrect! "); Return" error ";} // remember to have the get and set Methods public String getUpass () {return upass;} public void setUpass (String upass) {this. upass = upass;} public String getUname () {return uname;} public void setUname (String uname) {this. uname = uname ;}}

These are just a preliminary step. I will learn more about tags, dynamic methods, and so on later.

I hope this article will help you with Struts program design.

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.