您的位置:首页 > 编程语言 > Java开发

Struts2 入门训练1

2016-06-28 20:02 453 查看
之前有学了一点struts,不过才开始认真写代码,很基础。

首先是WebContent/WEB-INF/lib导入一些基本包

     asm-3.3.jar

    asm-commom-3.3.jar

    asm-tree-3.3.jar

   commons-fileupload-1.3.1.jar

   commons-io-2.2.jar

   commons-lang3-3.2.jar

   freemarker-2.3.22.jar

   javassist-3.11.0.GA.jar

   ognl-3.0.6.jar

  struts2-core-2.3.24.jar

  xwork-core-2.3.24.jar

 

然后WebContent下创建三个文件

  error.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>${error}<br></p>
</body>
</html> 
 success.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>${success}<br></p>
<h2>用户登录信息</h2>
用户名:${username}<br>
密码:${password}<br>
</body>
</html>

login.jsp
<%@ page language="java"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<form action="login" method="post">
用户名:<input type="text" name="username"/><br>
密码:<input type="password" name="password"/><br>
<input type="submit" value="登录"/>
</form>
</div>
</body>
</html>

然后WEB-INF/下创建一个web.xml配置文件,设置过滤器
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Two</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<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>

在src目录下创建一个包,cn.itcast.action
  创建一个java文件

Login.Action.java

package cn.itcast.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends 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;
}
@Override
public String execute() throws Exception{
ActionContext context = ActionContext.getContext();
if("itcast".equals(username) && "123".equals(password)){
context.put("username", username);
context.put("password", password);
context.put("success", "用户登录成功");
return SUCCESS;
}
else{
context.put("error", "用户登录失败");
return ERROR;
}
}
}


在src下创建一个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.3.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="cn.itcast.action.LoginAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>


到这里一个简单的登录验证框就出来了。
期间碰到了一些问题。

1  要点击server 里面的tomcat项目进去看到一个server option选择第二个框勾上,不然会出现错误,

Publish module contexts to separate XML files 这一项记得勾上

2  server Location里面一开始是勾上第一项的,有可能要选择第二项才可以,

 Use Tomcat installation这一项

3 还有就是一些包名打错检查了好一会儿。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java