Struts2 entry cases (Struts2 configuration and jump pages, and pages of information entered judgment)

Struts2 configuration:

<?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>
<filter>
<!-- <filter-name>名字保持一致 -->
<filter-name>struts</filter-name>
<!-- 1、struts2-core-2.3.37.jar
2、org.apache.struts2.dispatcher.ng.filter
3、StrutsPrepareAndExecuteFilter.class 中找 -->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
<!-- /*表示全部 -->
</filter-mapping>
</web-app>

Struts2 configuration file: the file name must be struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<-! Struts2-core-2.3.37.jar struts-default.xml find the configuration code hinting ->
! <DOCTYPE Struts the PUBLIC
"- // the Apache Software Foundation // DTD Struts the Configuration 2.3 // EN"
"HTTP : //struts.apache.org/dtds/struts-2.3.dtd ">
<Struts>

! <- If you change the code, you can not restart the server ->
<Constant name =" struts.configuration.xml.reload " = value "to true"> </ Constant>

<- name may be named their free, extends struts2 jar package, namespace:! default between / file path ->
<package name = "del" = the extends " default-Struts "namespace =" / ">

<-! name =" the Login "can take their casual, class is the path to your class files ->
<Action name =" the Login "class =" com.fn.action. Action ">

<-! page to jump to the specified jsp page ->
<result name="success">
/success.jsp
</result>
<result name="error">
/error.jsp
</result>
</action>
</package>
</struts>

class file code:

 

package com.fn.action;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class the Action the extends ActionSupport {
/ *
* inheritance ActionSupport class provides get, the method set, and acquires the input parameter (shortcut: the Shift + Alt + S)
* /
Private String name;
Private String pwd;

public String getName () {
return name;
}

 

public void setName(String name) {
this.name = name;
}

 

public String getPwd() {
return pwd;
}

 

public void setPwd(String pwd) {
this.pwd = pwd;
}

 

Execute String public () throws Exception {
// page is determined to perform data input; equals: Comparative
if ( "John Doe" .equals (name) && "123456" .equals (pwd)) {
return SUCCESS;
// SUCCESS, is built-in, can be used directly, you can also define their own: that the page jump success
} the else {
return ERROR;
// ERROR, is built-in, can be used directly, you can also define their own: that the page jump failure
}
}
}

jsp login page:

<% @ Page Language = "Java" contentType = "text / HTML; charset = UTF-. 8"
the pageEncoding = "UTF-. 8"%>
<! DOCTYPE HTML>
<HTML>
<head>
<Meta charset = "UTF-. 8 ">
<title> login page </ title>
</ head>
<body>
! <- jsp page input box information presented to the user login page
action: project name + struts.xml the action name
method: submission
name: going to get, set method names are consistent
->

<form Action = "Struts2 / the Login" method, = "POST">
<the p-> account name: <input type = "text" name = "name"> </ P>
<P> password: <INPUT type = "password" name = "pwd"> </ P>
<P> <INPUT type = "Submit" value = "登录">
</form>
</body>
</html>

jsp successful login page:

<% @ Page Language = "Java" contentType = "text / HTML; charset = UTF-. 8"
the pageEncoding = "UTF-. 8"%>
<! DOCTYPE HTML>
<HTML>
<head>
<Meta charset = "UTF-. 8 ">
<title> success page </ title>
</ head>
<body>
<- $ {name}:! EL expression, you can obtain user input and displayed on the page ->
<h2> $ {name Welcome }log in! </ H2>
</ body>
</ HTML>

Login failed jsp page:

<% @ Page Language = "Java" contentType = "text / HTML; charset = UTF-. 8"
the pageEncoding = "UTF-. 8"%>
<! DOCTYPE HTML>
<HTML>
<head>
<Meta charset = "UTF-. 8 ">
<title> error page </ title>
</ head>
<body>
user name or password you entered is wrong!
<! -
name the full path name directly write jsp page, do not jump in the past, they did not know how
such as this: <a href = login.jsp "> Click Back to the login page </a>
->
<A href = "$ {pageContext.request.contextPath} /login.jsp "> return login page </a>
</ body>
</ HTML>

Guess you like

Origin www.cnblogs.com/funing-z/p/12121592.html