struts2在模拟一个注册功能怎么最后显示不出,麻烦大哥哥 ,敲一下, 我才学这个有点不怎么懂,帮我一下
regist.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>regist</title>
</head>
<body>
<s:form action="loginPro">
<s:textfield name="user.name" label="输入名字" />
<s:textfield name="user.age" label="输入年龄:" />
<s:textfield name="user.marks" label="输入分数:" />
<s:submit value="submit" />
</s:form>
</body>
</html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>succPage</title>
</head>
<body>
<a href="user/listuser.action">viewLink</a>
</body>
</html>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>SUCCESS regist</title>
</head>
<body>
<table>
<s:iterator value="users" var="u">
<tr>
<td>${u.name}</td>
<td>${u.age}</td>
<td>${u.marks}</td>
</tr>
</s:iterator>
</table>
</body>
</html>
listuseraction.java
import org.struts.action.Login.serviceuser;
import org.struts.action.mian.User;
public class listuseraction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
List<User> users;
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public String execute() {
Logindata aa = new Logindata();
setUsers(aa.getall());
return SUCCESS;
}
}
RegistAction.java
package org.struts.action;
import org.struts.action.Login.serviceuser;
import org.struts.action.mian.User;
public class RegistAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String execue() throws Exception {
Logindata va = new Logindata();
Integer id = va.adduser(user);
if (id >0) {
return SUCCESS;
}
else
return ERROR;
}
}
serviceuser.java
package org.struts.action.Login;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.struts.action.mian.User;
public class serviceuser {
static List<User> userDb;
public Integer adduser(User user) {
userDb=new ArrayList<User>();
user.setId(new Random().nextInt(9999));
userDb.add(user);
for( User e:userDb){
System.out.print(e.getAge());
}
return user.getId();
}
public List<User> getall() {
// TODO 自动生成的方法存根
return userDb;
}
}
User.java
package org.struts.action.mian;
public class User {
private String name;
private int id;
private double age;
private int marks;
public User() {
}
public User(String name, String pass, int id, double age, int marks) {
super();
this.name = name;
this.id = id;
this.age = age;
this.marks = marks;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getAge() {
return age;
}
public void setAge(double age) {
this.age = age;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
}
struts.xml
<?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="default" namespace="/" extends="struts-default">
<action name="loginPro" class="org.struts.action.RegistAction">
<result >/WEB-INF/content/welcome.jsp</result>
<result name="error">/WEB-INF/content/error.jsp</result>
</action>
<action name="listuser" class="org.struts.action.listuseraction">
<result >/WEB-INF/content/success.jsp</result>
</action>
<action name="*">
<result>/WEB-INF/content/{1}.jsp </result>
</action>
</package>
<!-- Add packages here -->
</struts> struts2
[解决办法]
开发工具eclipse:
1 . 新建web项目,在webcontent/web-inf/lib 目录下导入struts2jar包
2 . 在src下新建一个struts.xml文件
3 . 配置web.xml 加入如下:
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
4. 新建三个页面首页,成功页面
index.jsp
<html>
<body>
<form action="" method="execute">
<input name="useranme" type="text">
<input name="password" type="password">
<input type="submit" value="提交">
</form>
</body>
</html>
success.jsp
<html><body>注册成功</body></html>
5. 写action
public void RegisterAction extends ActionSupport{
private Strring username;
private String password;
public void setUsername(String username){this.username = useranme;}
public String getUsername() {return username;}
//..省略password set get
public void execute(){
//向数据库啊插入数据,或者模拟一个即可
return SUCCESS;
}
}
5. 配置struts.xml
<package name="abc" extends="struts-default">
<action name="register" class="RegisterAction">
<result name="success">/success.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
6. wancheng
[解决办法]
贴下报错信息
[解决办法]
Logindata 把这个类贴出来亲。。,对了,你后建类的话,注意首字母大写规范一下。
[解决办法]
你这代码这么乱,怎么看啊,最少你也要说下报什么错