简单的说一下,第一个Struts2一个流程,第一个运用到Struts2程序可能开始有一些小问题,后来都积极的解决了。
1、先对web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>struts002</display-name>
<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>
2、登录jsp文件
/**
* 用了Struts2的标签,
* 详情第3行
**/
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<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>
</body>
</html>
3、结束界面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
username:${request.username}<br>
password:${request.password}<br>
</body>
</html>
4、进行struts.xml配置
/**
* new在src目录
*/
<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="sturts2" namespace="/" extends="struts-default">
<action name="login" class="com.test.action.LoginAction">
<result name="success">/result.jsp</result>
<result name="input">/login2.jsp</result>
<result name="failer">/login2.jsp</result>
</action>
</package>
</struts>
5、新建一个action
/**
* new在src目录中com.test.action下
*/
package com.test.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{ //继承的ActionSupport
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{
if("u".equals(this.getUsername().trim())&&"p".equals(this.getPassword().trim())){
return "success";
}
else{
this.addFieldError("username", "username or password error");
return "failer";
}
}
@Override
public void validate() {
if(null == this.getUsername() || "".equals(this.getUsername().trim())){
this.addFieldError("username", "username require");//会自动在字段上面提示错误信息
}
if(null == this.getPassword() || "".equals(this.getPassword().trim())){
this.addFieldError("password", "password require");//会自动在字段上面提示错误信息
}
}
}
运行login.jsp 会自动加载web.xml 输入信息提交后会找到 struts.xml里面的com.test.action.LoginAction 调用LoginAction.java 会通过validate()和execute()自动的判断