简单实现Struts2

第一次配置Struts2

刚接触Struts2,照着书上打代码出现了很多问题,记录下来以便以后翻阅.

遇到的问题

  1. ClassNotFound ,找不到org.apache.struts2.dispatcher.FilterDispatcher
    解决方法:在struts2-core-2.5.20.jar下找org.apache.struts2.dispatcher.filter,找到其中的StrutsPrepareAndExecuteFilter类名。
  2. 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> 
  1. 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>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值