You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring MVC@RequestMapping未触发,JSP无法显示数据库招标列表问题

Troubleshooting Your Unfired Spring MVC @RequestMapping

Let's walk through the common issues that could be preventing your @RequestMapping("/viewTendersByUser") from executing, and fix them one by one:

1. Verify Controller Class Annotations

First off, make sure your controller class is annotated with @Controller (not just a plain class!). Spring won't recognize it as a request handler without this annotation. For example:

@Controller
public class YourTenderController {
    // Your @RequestMapping method here
}

2. Check Request Path Matching

  • If your controller has a class-level @RequestMapping (e.g., @RequestMapping("/tender")), the full request path becomes /tender/viewTendersByUser, not just /viewTendersByUser. Double-check the URL you're accessing in the browser.
  • Spring MVC is case-sensitive by default. Ensure the URL you're using matches the exact case of your @RequestMapping value (e.g., don't use /viewtendersbyuser if your annotation is /viewTendersByUser).

3. Fix the Undefined name Variable in Your Controller

In your viewTender method, you're using userDao.getTenderByCreator(name) but name is never declared or populated! This is almost certainly causing an uncaught NullPointerException that's silently failing the request before your System.out statements run.

To test this quickly, replace name with a hardcoded value (like "testPublisher"), then see if your debug prints show up:

@RequestMapping("/viewTendersByUser")
public String viewTender(Model m) {
    System.out.println("in edit tender user controller");
    // Use a test value first to rule out name issues
    List<tender> tenderList = userDao.getTenderByCreator("testPublisher"); 
    m.addAttribute("tender", tenderList);
    System.out.println("in ctender after view tender");
    System.out.println(tenderList);
    return "viewTendersByUser";
}

Later, you'll need to properly populate name (e.g., get it from a logged-in user via HttpSession or Principal).

4. Secure and Fix Your DAO SQL Query

Your current SQL uses string concatenation, which is vulnerable to SQL injection and can break if name contains special characters (like single quotes). Switch to parameterized queries instead:

@Override
public List<tender> getTenderByCreator(String name) {
    // Use ? as a placeholder for the parameter
    String sql = "select * from tenderdb where publisher = ?";
    return jdbcTemplate.query(sql, new Object[]{name}, new RowMapper<tender>(){
        public tender mapRow(ResultSet rs, int row) throws SQLException {
            tender t = new tender();
            // Use column names instead of indexes to avoid mismatches if table schema changes
            t.settName(rs.getString("t_name")); // Replace with your actual column name
            t.setOpening(rs.getTimestamp("opening"));
            t.setClosing(rs.getTimestamp("closing"));
            t.setMinBid(rs.getDouble("min_bid"));
            t.settDesc(rs.getString("t_desc"));
            return t;
        }
    });
}

Also, confirm that the column names in your mapRow method match exactly what's in your tenderdb table (indexes like getString(1) are fragile if columns are reordered).

5. Fix JSP Template Errors

In your JSP, you're referencing ${tender.id} but your loop variable is row! This will cause EL errors (even if they're not showing up in the browser). Update those links to use row instead:

<td><a href="editTender/${row.id}">Edit</a></td>
<td><a href="deleteTender/${row.id}">Delete</a></td>

Also, make sure your JSP includes the JSTL core taglib at the top:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

6. Enable Debug Logging for Spring MVC

If none of the above fixes work, turn on DEBUG logging for Spring's web module to see exactly what's happening with your request. Add this to your logging configuration (e.g., logback.xml or log4j.properties):

<!-- For Logback -->
<logger name="org.springframework.web" level="DEBUG"/>

This will show you which request mappings Spring has registered, whether your request is matching any of them, and if any exceptions are being thrown during processing.

7. Confirm Component Scanning

Ensure your Spring configuration is scanning the package where your controller resides. In XML config:

<context:component-scan base-package="com.yourpackage.controllers"/>

Or in a Spring Boot application, make sure your main application class is in a parent package of your controller (or use @ComponentScan explicitly).


内容的提问来源于stack exchange,提问作者Abhinav Gupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:05:56