Simple example of struts2.0

Struts2.0 is a merged version of struts1.0 and WebWork2.2, which integrates the advantages of two popular MVC frameworks. It is a big improvement for the struts framework, but also a greater degree. The development process of the developer is simplified.
This example implements a simple login effect, the steps are as follows:
1. First, download the complete package of struts2.0.11 from the apache website (http://struts.apache.org/downloads.html). After extracting, you need to find the following files:
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.11.jar
xwork-2.0.4.jar
2, then, we will start the first example, the new web project struts2
3. Copy the jar files listed above to the project struts2\WEB-INF\lib, or add your own strust2.0 user library.
4, new login.jsp

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
<form action="login.action" method="post">
username:<input type="text" name="username" /><p>
password:<input type="password" name="password" /><p>
<input type="submit" name="Submit" value="submit" />
</form>
</body>
</html>

[color=red]
The difference between struts1 and struts2:
<form action="login.action" method="post">
Struts1 intercepts all requests sent by dos with servlet, and has ActionServlet control, which is decided to be processed by that Action.
[/color]
5, create a new result.jsp

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
username:${requestScope.username}<br>
password:${requestScope.password}
</body>
</html>

6, modify the web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>
struts2.0</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2.0</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2.0</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

[color=red]
The difference between struts1 and struts2:
Struts1 is a controller through servlet
Struts2 here uses Filter to filter the request sent by the client.
[/color]
7, create loginAction.java

package com.action;
public class loginAction {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

public String execute()throws Exception{
return "success";
}

}

[color=red]
The difference between struts1 and struts2:
Struts1 must inherit Action, override execute()
Struts2 does not need
[/color]
8, add struts.xml

<?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="struts2.0" extends="struts-default">
<action name="login" class="com.action.loginAction">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>

[color=red]
The difference between struts1 and struts2:
Struts1 in the project \WEB-INF\struts-config.xml configuration file
Struts2 in the project \src\struts.xml configuration file
[/color]
9, o, a simple example to complete, the following is code refactoring:
[b]Use the struts2.0 tag to add a checksum function[/b]
1, modify the login.jsp, use the struts2.0 label instead
Import tag <%@ taglib prefix="s" uri="/struts-tags"%>
Replace form form with struts2.0 tag

<s:form action="login">
<s:textfield name="username" label="username"></s:textfield>
<s:password name="password" label="password"></s:password>
<s:submit label="submit"></s:submit>
</s:form>

(Use the label to automatically wrap, the button is right-aligned by default)
2, modify loginAction.java
loginAction inherits ActionSupport
Rewrite the Validate() verification method

@Override
public void validate() {
if(null==this.getUsername()||"".equals(this.getUsername().trim())){
this.addFieldError("username", "username error");//Add error message
}
if(null==this.getPassword()||"".equals(this.getPassword().trim())){
this.addFieldError("password", "password error");//Add error message
}
}

3, modify struts.xml, add the page returned by error

<?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="struts2.0" extends="struts-default">
<action name="login" class="com.action.loginAction">
<!-- Add the page returned by the error -->
<result name="input">/login.jsp</result> <result name="success">/result.jsp</result>
</action>
</package>
</struts>

[color=red]
The difference between struts1 and struts2:
Struts1 needs to add the <html:errors/> tag to the form to prompt for error handling information.
Struts2 does not need
[/color]
[b]The matching username is hello and the password is world[/b]
1, modify the execute() method under loginAction.java

public String execute()throws Exception{
if("hello".equals(this.getUsername().trim())&&"world".equals(this.getPassword().trim())){
return "success";
}
else{
this.addFieldError("username", "username or password error");//Defined error message above username
return "failer";
}

}

2, modify struts.xml, increase the failer map

<?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="struts2.0" extends="struts-default">
<action name="login" class="com.action.loginAction">
<result name="input">/login.jsp</result>
<result name="success">/result.jsp</result>
<!-- Increase the failer map -->
<result name="failer">/login.jsp</result>
</action>
</package>
</struts>

3, all completed, copy the finished web application struts2 to tomcat

Intelligent Recommendation

struts2.0 (one)

Struts2.0 action address conversion Configure Struts core filter Create struts.xml Write Action Struts process Struct constant configuration struts.xml modularization Struts Action class Action access...

Struts2.0 preliminary

Summary of the preliminary learning of Struts2.0 Struts2 is a Web application framework based on the MVC design pattern, which is essentially equivalent to a servlet. In the MVC design pattern, Struts...

The jar package required by Struts2.0

mainly needs the following jar packages: struts2-core-2.1.6.jar xwork-2.1.2.jar ognl-2.6.11.jar freemarker-2.3.13.jar commons-logging.jar commons-fileupload.jar commons-io.jar log4j-1.2.15.jar Log4j.p...

Struts2.0 preliminary study (1)

Promotion: The Taobao shop opened by yourself, the main car accessories, accessories, you can go in and see, it is best to help the buddy to promote Store name:Xuanyuan car accessories   &nb...

More Recommendation

Struts2.0 processing flow

One: Review the processing flow of struts2.0...

Data transfer in Struts2.0

[b]Struts2.0 direct data transfer: [/b][align=center][/align] 1. Pass data between action and action: In the first action, use the execute() method: //getUser() is the encapsulation of the parameter n...

Struts2.0 file upload principle

Struts2 file upload defaults to the apache file upload package, the new processing upload Servlet is as follows:     Create a new folder under WebRoot: upload, an upload folder in the server...

Jquery with struts2.0 paging

Java code Need to import the file,: Java code     Java code...

2010.06.23———spring+struts2.0 configuration

2010.06.23——— spring+struts2.0 configuration web.xml applicationContext.xml spring configuration file Struts.xml struts2 configuration file Inject dao inside jdbcTemplage, service in...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top