Struts2 HelloWorld
第一次配置Struts2
刚接触Struts2,照着书上打代码出现了很多问题,记录下来以便以后翻阅.
遇到的问题
- ClassNotFound ,找不到org.apache.struts2.dispatcher.FilterDispatcher
解决方法:在struts2-core-2.5.20.jar下找org.apache.struts2.dispatcher.filter,找到其中的StrutsPrepareAndExecuteFilter类名。 - Method create for action login is not allowed!,action方法不被允许,这是因为 struts2.5 为了提升安全性,添加了 allomethod 这么个玩意。
解决方法:在配置文件中添加:"regex:.*
<package name="teststruts" extends="struts-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="login" class="test.LoginAction">
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
</package>
- Wrong method was defined as an action method: create,项目里面有JAR包 struts2-rest-plugin-2.5.XXX,删了就好了
HelloWorld代码
LoginAction.java
package test;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String username;//账号
private String password;//密码
//getters & setters
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{
System.out.println(username+" "+password);
if(username.equalsIgnoreCase("admin") && password.equalsIgnoreCase("123456")){
return "success";
}
else
return "error";
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="teststruts" extends="struts-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="login" class="test.LoginAction">
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>登录</title>
</head>
<body>
<form action="login" method="post">
登录<br />
账号:<input type="text" name="username" /><br />
密码:<input type="password" name="password" /><br />
<input type="submit" value="登录" />
</form>
</body>
</html>
error.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>error</title>
</head>
<body>
error!
</body>
</html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Welcome</title>
</head>
<body>
<s:property value="username"/>,welcome!
</body>
</html>
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_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>teststruts</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>
</web-app>