代码显示404错误 - 找不到请求的资源。我已多次浏览代码,但我没有发现任何线索。那我怎么解决呢?请帮忙。
的login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form:form action="inslogin" method="get" modelAttribute="log">
<form:hidden path="id"/>
<table>
<tr>
<td>First name:</td>
<td><form:input path="fname"/></td>
</tr>
<tr>
<td>Last name:</td>
<td><form:input path="lname" /></td>
</tr>
<tr>
<td><input type="submit" value="insert"></td>
</tr>
</table>
</form:form>
</body>
</html>
logincontroller.java
package com.dipen.controller;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.dipen.dao.logindao;
import com.dipen.vo.loginvo;
@Controller
public class logincontroller {
@Autowired
logindao logind;
@Autowired
loginvo loginv;
@RequestMapping("/")
public ModelAndView home(){
return new ModelAndView("login","log",new loginvo());
}
@RequestMapping(value="/inslogin", method = RequestMethod.GET)
public ModelAndView insertname(@ModelAttribute loginvo loginv){
System.out.println("start of insertname method");
int i = loginv.getId();
if(i==0){
logind.insertlogin(loginv);
System.out.println("if part");
}
else{
logind.updatelogin(loginv);
System.out.println("else part");
}
System.out.println("end of insertname method");
ModelAndView mv1=new ModelAndView("welcome");//file name
return mv1;
}
@RequestMapping(value="/search", method = RequestMethod.GET)
public ModelAndView search(@ModelAttribute loginvo loginv){
System.out.println("start of search method");
List ls = logind.searchdata(loginv);
System.out.println("done");
ModelAndView mv=new ModelAndView("search");
mv.addObject("abc",ls);
System.out.println("sss"+ls.size());
System.out.println("end of search method");
return mv;
}
@RequestMapping(value="/delete.html", method = RequestMethod.GET)
public ModelAndView delete(@ModelAttribute loginvo loginv,@RequestParam int id){
loginv.setId(id);
logind.delete(loginv);
ModelAndView mv1=new ModelAndView("redirect:/search");
return mv1;
}
@RequestMapping(value="/login.html", method = RequestMethod.GET)
public ModelAndView update(@ModelAttribute loginvo loginv,@RequestParam int id){
System.out.println("start of update method");
ModelAndView mv1 = new ModelAndView("login");
loginvo r = this.logind.getRowById(id);
System.out.println("update controller : "+r.getFname());
mv1.addObject("log",this.logind.getRowById(id));
return mv1;
/*List ls = new ArrayList();
loginv.setId(id);
ls = logind.searchdatabyid(loginv);*/
/*System.out.println(ls.size());*/
}
}
logindao.java
package com.dipen.dao;
import java.util.List;
import java.util.ArrayList;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.dipen.vo.loginvo;
@Repository
@Transactional
public class logindao {
@Autowired
SessionFactory sessionFactory;
public String insertlogin(loginvo loginv) {
try {
Session session = this.sessionFactory.openSession();
Transaction tr = session.beginTransaction();
session.save(loginv);
tr.commit();
session.close();
System.out.println("dao inserlogin");
} catch (Exception e) {
System.out.println(e.getCause());
}
return "Record Inserted...";
}
public List searchdata(loginvo loginv) {
List ls = new ArrayList();
try {
Session session = sessionFactory.getCurrentSession();
Query q = session.createQuery("from loginvo");
ls = q.list();
System.out.println("dao searchdata");
} catch (Exception e) {
e.printStackTrace();
}
return ls;
}
/* public List searchdatabyid(loginvo loginv) {
List ls = new ArrayList();
try {
Session session = sessionFactory.getCurrentSession();
Query q = session.createQuery("from loginvo where id='"+loginv.getId()+"'");
ls = q.list();
System.out.println("dao searchdatabyid");
} catch (Exception e) {
e.printStackTrace();
}
return ls;
}
*/
public loginvo getRowById(int id){
Session session = sessionFactory.getCurrentSession();
loginvo lv = (loginvo)session.load(loginvo.class,id);
System.out.println("ID :"+lv.getId());
System.out.println("Fname :"+lv.getFname());
return lv;
}
public void delete(loginvo loginv) {
try {
Session session = sessionFactory.openSession();
/* Session session = sessionFactory.getCurrentSession(); */
/* Transaction tr=session.beginTransaction(); */
Query q = session.createQuery("delete loginvo where id='"+loginv.getId()+ "'");
q.executeUpdate();
/* tr.commit(); */
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public String updatelogin(loginvo loginv) {
try {
System.out.println("inside updatelogin");
Session session = this.sessionFactory.openSession();
Transaction tr = session.beginTransaction();
session.update(loginv);
tr.commit();
session.close();
System.out.println("dao updatelogin");
} catch (Exception e) {
System.out.println(e.getCause());
}
return "Record Updated...";
}
}
loginvo.java
package com.dipen.vo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class loginvo {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private int id;
@Column(name="fname")
private String fname;
@Column(name="lname")
private String lname;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
}
的welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Did it !!!</h1>
<form:form action="search" method="get" modelAttribute="searchall">
<input type="submit" value="search">
</form:form>
</body>
</html>
search.jsp的
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table border="1px">
<tr>
<td>Id</td>
<td>Firstname</td>
<td>Lastname</td>
<td>Update</td>
<td>Delete</td>
</tr>
<c:forEach items="${abc}" var="j">
<tr>
<td>${j.id}</td>
<td>${j.fname}</td>
<td>${j.lname}</td>
<td>
<a href="login.html?id=${j.id}">Update</a>
</td>
<td>
<a href="delete.html?id=${j.id}">delete</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Spring_insert</display-name>
<!-- <welcome-file-list>
<welcome-file>home.jsp</welcome-file>
</welcome-file-list> -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- <listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>-->
</web-app>
的applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="com.dipen" />
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.dipen.vo.loginvo</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="logindao" class="com.dipen.dao.logindao">
</bean>
<bean id="loginvo" name="Student" class="com.dipen.vo.loginvo">
</bean>
</beans>
答案 0 :(得分:1)
jsp
下有WEB-INF
个文件,但您的prefix
似乎不正确。用
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>