JAVA Struts2 Building

Source: Internet
Author: User

Java Struts 2 Build

1.web Engineering

2. Copy the jar package used by struts2 to the Webcontent/webinf/lib folder. Under

Config under 3.webcontent under Web. xml:

<?xml version= "1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id= "Webapp_ ID "version=" 3.0 ">
<display-name>struts2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>

4.java resources/src/Jian Struts.xml

<?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>
namespase--> namespaces

extends= "Struts-default"-----> Default Inheritance

<interceptors>----> Filters

class= "Cn.stu.strust.MyTime"-----> Filter Package
<package namespace= "/" Name= "Cn.stu.strust" extends= "Struts-default" >
<interceptors>
<interceptor name= "Mytime" class= "Cn.stu.strust.MyTime" ></interceptor>

</interceptors>

Map the corresponding

/calss= "Cn.stu.strust"//action---> where the Package class is located
<action name= "Login" class= "Cn.stu.strust.loginaction" >

<interceptor-ref name= "Mytime" ></interceptor-ref>//loading a custom filter
<interceptor-ref name= "defaultstack" ></interceptor-ref>//load default filter
<result name= "Success" >/success.jsp</result>
<result name= "Error" >/error.jsp</result>
<result name= "Input" >index.jsp</result>//validation returned
</action>



</package>
</struts>

5. Package structure is as follows:

6.logginaction.java

Package cn.edu.tsu.se.sturts;

Import Java.util.Map;

Import javax.servlet.http.HttpSession;

Import Org.apache.struts2.ServletActionContext;
Import Org.apache.struts2.interceptor.SessionAware;

Import Cn.edu.tsu.se.sturts.entity.User;

Import Com.opensymphony.xwork2.ActionSupport;


public class Loginaction extends Actionsupport implements sessionaware{
private user user;
Map<string,object> session;
Public String Login () throws Exception {
TODO auto-generated Method Stubyste
System.out.println ("action doing");
if (USER.GETN1 (). Equals (User.getpwd ())) {
map<string,object> session = Actioncontext.getcontext (). GetSession ();
Session.put ("Name", USER.GETN1 ());
HttpSession session = Servletactioncontext.getrequest (). GetSession ();
Session.setattribute ("Name", USER.GETN1 ());
Return "Success";
}else{
return "error";
}

}



@Override
public void Validate () {
TODO auto-generated Method Stub
if (User.getpwd (). Equals ("1111")) {
This.addfielderror ("User.pwd", "Password Too simple");
}
}



public void SetUser (user user) {
This.user = user;
}

Public User GetUser () {
return user;
}



@Override
public void Setsession (map<string, object> arg0) {
TODO auto-generated Method Stub
session = arg0;
}





}

JAVA Struts2 Building

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.