Home>

1.New web project, name:struts2demo;

2. Create a user library struts2, containing a minimum of 6 jar files for a minimum of struts2;

Actually, for myeclipse8 and above, Is not necessary, Because it directly supports struts2. No additional package is needed.

3.Use the build path to add the struts2 library;

4.Add the following configuration in 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 ">
  <!-Configuration of struts2->
  <filter>
    <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. Establish action

package com.yenange.action;
import com.opensymphony.xwork2.action;
public class loginaction implements action {
  private string uname;//name in the form
  private string upass;//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, there must be 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"%>
<html>
  <head>
    <title>my jsp "index.jsp" starting page</title>
  </head>
  <body>
    <form action="login">
      Username:<input type="text" name="uname"><br>
      Password:<input type="text" name="upass"><br>
      <input type="submit" value="login" />
    </form>
  </body>
</html>

welcome.jsp:

<%@page language="java" import="java.util. *" Pageencoding="gbk"%>
<html>
  <head>
    <title>my jsp "welcome.jsp" starting page</title>
  </head>
  <body>
    welcome struts2
  </body>
</html>

fail.jsp:

<%@page language="java" import="java.util. *" Pageencoding="gbk"%>
<html>
 <head>
  <title>my jsp "welcome.jsp" starting page</title>
 </head>
 <body>
Something went wrong!
</body>
</html>

7. Configure struts.xml. (For myeclipse6, you can only copy from elsewhere)

<?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">
      <result name="success">/welcome.jsp</result>
      <result name="error">/fail.jsp</result>
    </action>
  </package>
  <!-Add packages here->
</struts>

8.That's it.Ready to run, Is it very simple?

Here's how to get things like 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.struts2.util.servletcontextaware;
import com.opensymphony.xwork2.action;
public class loginaction implements action, servletrequestaware, sessionaware, servletcontextaware {
  private string uname;//name in the form
  private string upass;//Password in the form
  httpservletrequest request;//Common request
  map session;//Common sessions are different, but do not need to be managed
  servletcontext application;//Common application
  //The second method of taking;
  httpservletrequest request2=servletactioncontext.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, there must be 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 preliminary, Learn more about labels later, Things like dynamic methods.

jsp
  • Previous Detailed method of sorting list elements in Java
  • Next jquery with index button and automatic carousel switching special effects