gpt4 book ai didi

java - 如何从Servlet改为Struts2?

转载 作者:行者123 更新时间:2023-12-01 13:18:02 25 4
gpt4 key购买 nike

我对struts2还很陌生。我的 Web 应用程序中有几个 Servlet,我想更改它以便使用 Struts2 框架。我该如何做到这一点并在代码中保持相同的逻辑?我也在使用 Hibernate,并且还想将 Struts2 与 Hibernate 一起使用。

例如,我的 Web 应用程序中有这个 servlet:

package com.webapp.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;

import com.webapp.entity.UserEntity;
import com.webapp.util.HibernateUtil;

/**
* Servlet implementation class SignUpServlet
*/
@WebServlet("/SignUpServlet")
public class SignUpServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public SignUpServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doProcess(request, response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doProcess(request, response);
}


protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("in process");
String userName = request.getParameter("userName");
String password = request.getParameter("password");
String email = request.getParameter("email");
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String contactNo = request.getParameter("contactNo");
String address = request.getParameter("address");

UserEntity userEntity = new UserEntity();

userEntity.setUserName(userName);
userEntity.setPassword(password);
userEntity.setEmail(email);
userEntity.setFirstName(firstName);
userEntity.setLastName(lastName);
userEntity.setAddress(address);
userEntity.setContactNo(contactNo);

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();

session.save(userEntity);

session.getTransaction().commit();

response.sendRedirect("jsp/login.jsp");
}
}

还有模型

package com.webapp.entity;

import java.io.Serializable;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
@Entity
@Table(name = "Users")
public class UserEntity implements Serializable{

/**
*
*/
private static final long serialVersionUID = 1L;

@Id
@Basic(optional = false)
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name = "userId")
private Long userId ;

@Column(name = "userName")
private String userName;

@Column(name = "password")
private String password;

@Column(name = "roleId")
private Long roleId;

@Column(name = "email")
private String email;

@Column(name = "firstName")
private String firstName;

@Column(name = "lastName")
private String lastName;

@Column(name = "address")
private String address;

@Column(name = "contactNo")
private String contactNo;





public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getContactNo() {
return contactNo;
}

public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}

public Long getUserId() {
return userId;
}

public void setUserId(Long userId) {
this.userId = userId;
}

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 Long getRoleId() {
return roleId;
}

public void setRoleId(Long roleId) {
this.roleId = roleId;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

}

View 是 SignUp.jsp

<head>
</head>
<body>

<br></br>
<br></br>
<table width="" border="0" align="center" cellpadding="0"
cellspacing="0" class="maintable">
<tr>
<td valign="top" align="center" class="middlesection">
<table width="96%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
&nbsp;
</td>
</tr>
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<table width="100%" border="0" cellspacing="0"
cellpadding="0">
<tr>
<td align="left" valign="middle">
<h1>
Sign Up
</h1>
</td>
<td align="right" valign="bottom">
<table width="0%" border="0" cellspacing="0"
cellpadding="0">
<tr>
<td align="center" style="color: #00FF00">
&nbsp;
</td>
<td>
<a href="#" class="buttonash" onclick="resetForm();"><span>
reset </span> </a>
</td>
<td width="5"></td>
<td>
<a href="#" class="buttonash" onclick="submit()"><span>
SignUp </span> </a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="bottomboder">
<img src="/WebApp/images/ashish.gif" height="1" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
&nbsp;
</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>
<div class="tab-content-out" style="clear: both">
<div id="tabs" class="tabs">
<div id="TabbedPanels1" class="TabbedPanels">

<div
style="background: url(/WebApp/images/tabhdbg.gif) top repeat-x #ffffff; height: 70px;"
class="TabbedPanelsTabGroup">
<div style="width: 1px; float: left;">
<img src="/WebApp/images/tabhdline.gif" />
</div>
<div class="navi"
style="padding-left: 7px; padding-top: 9px;">
<div style="color: #4B5974; font-size: 24px;"
align="center">
<b> SignUp Form</b>
</div>
</div>

<div style="width: 1px; float: right;">
<img src="images/tabhdline.gif" />
</div>
</div>
</div>
<div class="TabbedPanelsContentGroup">
<div class="TabbedPanelsContent">
<div style="border: 0;" class="tab-content">
<div class="entry-edit">
<div class="tebelhd" align="left">
General Information
</div>
<div class="fieldset" id="_generalbase_fieldset">
<div class="hor-scroll" align="left">
<table class="form-list" cellspacing="0" align="center">
<tbody>
<form method="get" id="addCustomer" name="addCustomer" action="/WebApp/SignUpServlet" >
<table>
<tr>
<td>
<label>First Name</label>
</td>
<td>
<input type="text" name="firstName" required size="51" class="required-entry required-entry input-text"/>
</td>
</tr>
<tr>
<td>
<label>Last Name</label>
</td>
<td>
<input type="text" name="lastName" size="51" class="required-entry required-entry input-text"/>
</td>
</tr>
<tr>
<td>
<label>Adddress</label>
</td>
<td>
<input type="text" name="address" size="51" class="required-entry required-entry input-text"/>
</td>
</tr>
<tr>
<td>
<label>Contact No.</label>
</td>
<td>
<input type="text" name="contactNo" size="51" class="required-entry required-entry input-text"/>
</td>
</tr>
<tr>
<td>
<label>Email</label>
</td>
<td>
<input type="text" name="email" size="51" class="required-entry required-entry input-text"/>
</td>
</tr>
<tr>
<td>
<label>UserName</label>
</td>
<td>
<input type="text" name="userName" size="51" class="required-entry required-entry input-text"/>
</td>
</tr>
<tr>
<td>
<label>Password</label>
</td>
<td>
<input type="password" name="password" size="51" class="required-entry required-entry input-text"/>
</td>
</tr>
</table>
</form>

</tbody>
</table>

</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>

</body>

</html>

最佳答案

简而言之:

  1. 不要一开始就随机编辑应用程序中的内容,这会导致痛苦的世界。
  2. Read a Tutorial ,至少获得有关工作流程的最小概念。
  3. 使用 Maven 和 Struts2 原型(prototype)之一(可能是 Blank Convention Archetype)创建一个示例 Struts2 项目;让它运行,然后尝试改变一些东西,至少花一些时间来了解基础知识。
  4. 针对每个疑问深入研究 Struts 文档,深入了解基础知识。永远记住,每个操作在被调用之前和之后都通过拦截器堆栈(可定制的拦截器列表,每个拦截器执行不同的业务)运行,并且操作是按用户每个请求创建的,因此是线程安全的,而拦截器不是线程安全的。

enter image description here

经过几天的乐趣后,您可以开始编辑自己的大项目。

在旧版本中,Struts2 将 Actions 映射到一个名为 struts.xml 的文件中,但使用 Convention 插件则无需这样做,Actions 将根据其方法名称进行映射(或使用 @Action 注释)。

然后

  • 摆脱 doGet()doPost() 方法
  • doProcess() 更改为 newActionName(),这将成为操作的名称,或者放入 @Action(value="newActionName") 每个 doProcess() 方法
  • 映射操作的结果(正常或redirectAction),并删除对 servlet 内的 response 对象的所有引用。

如果你的项目很大,你不了解Struts2,并且你会尝试在没有花几天时间学习的情况下开始这种转换,那么你会在以后损失更多的天数,并得到更糟糕的结果。

关于java - 如何从Servlet改为Struts2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22310687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com