SSH 框架常见错误

时间:2024-04-19 17:37:50

新手学习SSH框架的时候总会遇到很多问题,一碰到404 或者500错误就不知道怎么解决。

404错误是很常见的一个错误,如果没有用框架基本上只可能是没有这个路径或者文件,但是用了框架之后404的原因就比较多了

404很有可能是struts配置,hibernate配置及其映射文件、applicationContext配置文件错误

首先是xml文件通不过校验,把xml文件拖到浏览器当中,如果通不过校验会提示你哪一行有问题

xml文件一定要嵌套,struts文件里面的action调用的方法一定要有,不然会报404或500错误

给一个struts配置给大家参考一下

<?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">
<!-- stu --> <action name="list" class="com.hngc.action.CantonAction" method="list">
<result name="success">/listCanton.jsp</result>
</action> </package> </struts>

  对应的action类如下

package com.hngc.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.hngc.entity.Canton; import com.hngc.service.BaseService;
import com.hngc.service.impl.CantonServiceImpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject; public class CantonAction extends ActionSupport {
private HttpServletRequest request= ServletActionContext.getRequest();//
private HttpServletResponse response= ServletActionContext.getResponse();
private BaseService<Canton> cantonService;
private Canton canton; public BaseService<Canton> getCantonService() {
return cantonService;
}
public void setCantonService(BaseService<Canton> cantonService) {
this.cantonService = cantonService;
}
public Canton getCanton() {
return canton;
}
public void setCanton(Canton canton) {
this.canton = canton;
} public String list() {
ActionContext context = ActionContext.getContext();
context.put("list", cantonService.list());
return SUCCESS;
}
public String save() {
ActionContext context = ActionContext.getContext();
cantonService.save(canton); return SUCCESS;
} }

要注意这里的 cantonService要和applicationContext里面的bean相对应,get set方法最好自动生成

有些人习惯拷贝之前的代码,忘记改get set方法名,这个网页会报500错误

canton 是实体类对象,需要get set方法,并且网页s标签也要对应,表单里面的name也要对应

1 <input name="canton.code" type="text" id="title" size="20">

hibernate配置一般不会出错,但是映射文件很容易写错

 1 <?xml version="1.0" encoding="utf-8"?>
2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4 <!--
5 Mapping file autogenerated by MyEclipse Persistence Tools
6 -->
7 <hibernate-mapping>
8 <class name="com.hngc.entity.Canton" table="canton" schema="dbo" catalog="LandDB">
9 <id name="code" type="java.lang.String">
10 <column name="code" />
11 <generator class="assigned" />
12 </id>
13 <property name="cname" type="java.lang.String">
14 <column name="cname" not-null="true" />
15 </property>
16 <property name="abb" type="java.lang.String">
17 <column name="abb" not-null="false" />
18 </property>
19 <property name="govName" type="java.lang.String">
20 <column name="govName" not-null="false" />
21 </property>
22 <property name="landName" type="java.lang.String">
23 <column name="landName" not-null="false" />
24 </property>
25 <property name="parentCode" type="java.lang.String">
26 <column name="parentCode" not-null="false" />
27 </property>
28 </class>
29 </hibernate-mapping>

这是一个完整的映射文件,容易错的地方有主键主键生产策略

<generator class="assigned" />

数据库字段可空,但是却写了 not-null="true"

类型不匹配 数据库字段为int t但是却写ype="java.lang.String

字段不匹配  <column name="parentCode" not-null="false" />

这个parentCode 是数据库中的字段

applicationContext的错误可以一个一个的注释掉测试

service action当中的get set方法要记得改名字,最好自动生成

这一期只是大概说一下错误的解决方法

之后后慢慢讲解具体的报错,有遇到问题的也可以留言